Replacing text for all files in a directory

Alex Martelli aleaxit at yahoo.com
Tue Aug 7 16:34:33 EDT 2001


"Kemp Randy-W18971" <Randy.L.Kemp at motorola.com> wrote in message
news:mailman.997212435.27088.python-list at python.org...
> Since I don't write Python every day, here is a simple question.
> I have a directory called test, containing files A, B, ..., Z, and I want
to
> replace all occurrences of the text "Have a good day" to "jump off a
cliff."
> This is a comic example, but I need to do something similar in real life.
> How can I do this in Python, running on Unix?

import fileinput, glob, os

os.chdir('test')
for line in fileinput.input(glob.glob('*'), inplace=1):
    print line.replace("Have a good day","jump off a cliff."),


Alex






More information about the Python-list mailing list