String find and replace
Thomas Güttler
guettler at thomas-guettler.de
Wed Aug 27 08:03:47 EDT 2003
hokiegal99 wrote:
> Thanks for the explanation, I can make it work this way:
>
> import os, string
> setpath = raw_input("Enter the path: ")
> for root, dirs, files in os.walk(setpath):
> fname = files
> x = 'THIS'
> y = 'THAT'
> for fname in files:
> myfile = file(os.path.join(root,fname), 'r')
> mystr = myfile.read()
> myfile.close()
> search = string.find(mystr, x)
> if search >=1:
> string.replace(mystr, x, y)
> print "Replacing", x, "with", y, "in", fname
>
> If only I could actually make the change to the files! It works in
> theory, but not in practice ;) Anyone recommend how to actual write the
> change to the file? I'm new to this, so be kind.
I once wrote a replace recursive script. Maybe it helps you:
http://www.thomas-guettler.de/scripts/replace-recursive.py.txt
thomas
More information about the Python-list
mailing list