Search & Replace

DataSmash rdh at new.rr.com
Thu Oct 26 17:27:11 EDT 2006


Hello,
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.
Can someone help me out.
Thanks!

# Search & Replace
file = open("text.txt", "r")
text = file.read()
file.close()

file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()




More information about the Python-list mailing list