Script Discussion & Critique

Sean Ross sross at connectmail.carleton.ca
Wed Aug 27 20:28:58 EDT 2003


"hokiegal99" <hokiegal99 at hotmail.com> wrote in message
news:3F4D4402.3060209 at hotmail.com...

Here's a some trivial (mostly cosmetic) changes:

# multi-line string
print """
******************************************************
   Three Easy Steps to a Recursive Find and Replace
******************************************************
"""
text = raw_input("1. Enter the string that you'd like to find: ")
replacement = raw_input("\n2. What would you like to replace '%s' with:
"%text)
path = raw_input("\n3. Enter the path where the program should run: ")
print
# " " is not required.

for root, dirs, files in os.walk(path):
     for fname in files:
        filename = os.path.join(root,fname)
        fd = file(filename, 'r')
        data = fd.read()
        fd.close()
        if string.find(data, text) >=1:
           data = data.replace(text, replacement)
           fd = file(filename, 'w')
           fd.write(data)
           fd.close()
           print "Replacing '%s' with '%s' in '%s'" % (text, replacement,
fname)

print """
**********
   Done
**********
"""

# Note: I haven't tested the changes.

Hope that's useful,
Sean






More information about the Python-list mailing list