Replacing text for all files in a directory

Simon Brunning sbrunning at bigfoot.com
Tue Aug 14 05:50:54 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?

You have had a lot of good answers to this, but I'll pitch in with
mine. I have a script here
<http://www.brunningonline.net/simon/python/multirep.py> which will
allow you to do this and more. It allows you to make multiple
replacements at once (i.e. you could replace 'foo' with 'spam' *and*
'bar' with 'lard' on one pass), and to optionally replace only whole
words.

HTH.

Cheers,
Simon B.



More information about the Python-list mailing list