Script Discussion & Critique

hokiegal99 hokiegal99 at hotmail.com
Thu Aug 28 02:54:06 CEST 2003


Something odd that I've discovered about this script is that it won't 
replace strings at the far left side of a text file *unless* the string 
has one or more spaces before it. For example:

THIS (won't be replace with THAT)
  THIS (will be replaced with THAT)
   THIS (will be replaced with THAT)
                        THIS (will be replaced with THAT)


> #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