(gulp) newbie question - portablity of python
Moshe Zadka
moshez at math.huji.ac.il
Sun May 14 16:32:55 EDT 2000
On Sun, 14 May 2000, Keygrass wrote:
> So ... my question is this: Is this really too good to be true?
Nope
> Basically, I want a build a program that will replace words and number on
> another file. For example, if I had a file called "Outdoor_sports" that
> contained the word "baseball bat", I would want my program to change it so
> it would say "football".
Here it is, in its entirety.
-------
#!/usr/local/bin/python
import string, sys
def replace_word_in_file(file, word, replacement):
f = open(file)
s = f.read()
f.close()
s = string.replace(s, word, replacement)
f = open(file, 'w')
f.write(s)
f.close()
if __name__ == '__main__':
if len(sys.argv) < 4:
exit(1)
for file in sys.argv[3:]:
replace_word_in_file(sys.argv[1], sys.argv[2], file)
-------
Save it as "replace.py", and it will work on pretty much anything.
For 10 more minutes of work, you can also write a Tk GUI which will work
on Windows an UNIX (and on MacOS, with a little coaxing)
--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com
More information about the Python-list
mailing list