Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String find and replace

Thread Tools

String find and replace

Posts: n/a
Thomas GŁttler wrote:
> I once wrote a replace recursive script. Maybe it helps you:
> thomas

Yes that's very helpful, but a bit too complex for me at this point in
time. Here is the script that I put together from all of the responses I
recieved from the list:

#Thanks to comp.lang.python
import os, string
print " "
print "************************************************* *****"
print " Three Easy Steps to a Recursive find and Replace "
print "************************************************* *****"
print " "
x = raw_input("1. Enter the string that you'd like to find: ")
print " "
y = raw_input("2. What would you like to replace %s with: " %x)
print " "
setpath = raw_input("3. Enter the path where the prgroam should run: ")
print " "
for root, dirs, files in os.walk(setpath):
fname = files
for fname in files:
inputFile = file(os.path.join(root,fname), 'r')
data =
search = string.find(data, x)
if search >=1:
data = data.replace(x, y)
outputFile = file(os.path.join(root,fname), 'w')
print "Replacing", x, "with", y, "in", fname
print " "
print "**********"
print " Done "
print "**********"
print " "

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
Interactive Find and Replace String Patterns on Multiple Files Xah Lee Java 0 06-14-2006 08:54 PM
Interactive Find and Replace String Patterns on Multiple Files Xah Lee Python 0 06-14-2006 08:54 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM