In <[EMAIL PROTECTED]>, DataSmash wrote: > I need to search and replace 4 words in a text file. > Below is my attempt at it, but this code appends > a copy of the text file within itself 4 times.
Because you `write()` the whole text four times to the file. Make the 4 replacements first and rebind `text` to the string with the replacements each time, and *then* write the result *once* to the file. > # Search & Replace > file = open("text.txt", "r") > text = file.read() > file.close() > > file = open("text.txt", "w") text = text.replace("Left_RefAddr", "FromLeft") text = text.replace("Left_NonRefAddr", "ToLeft") # ... file.write(text) file.close() Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list