Script Discussion & Critique

hokiegal99 hokiegal99 at hotmail.com
Wed Aug 27 19:51:30 EDT 2003


derek / nul wrote:
> here will do

OK, I have a few questions about the script at the end of this message:

Is this script written well? Is it a good design? How could it be made 
better (i.e. write to a file exactly what the scipt did)? I hope to use 
it to recursively find a replace strings in all types of files (binary 
and text).

Thanks for any ideas, pointers or critique... most of the ideas behind 
the script came 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 = inputFile.read()
       inputFile.close()
       search = string.find(data, x)
       if search >=1:
          data = data.replace(x, y)
          outputFile = file(os.path.join(root,fname), 'w')
          outputFile.write(data)
          outputFile.close()
          print "Replacing", x, "with", y, "in", fname
print " "
print "**********"
print "   Done   "
print "**********"
print " "





More information about the Python-list mailing list