String find and replace
guettler at thomas-guettler.de
Wed Aug 27 14:03:47 CEST 2003
> 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()
> 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:
More information about the Python-list