String find and replace

Erik Max Francis max at alcyone.com
Tue Aug 26 22:48:24 EDT 2003


hokiegal99 wrote:

> I hate to answer my own post, but I think I understand what I'm doing
> wrong. I'm finding and replacing 'this' with 'that' in the varible
> named
> mystr, not the actual files. So, how would I go about making the
> change
> to the actual files instead of a variable that contains their content?

Easy:

	inputFile = file(filename, 'r')
	data = inputFile.read()
	inputFile.close()
	data = data.replace(this, that)
	outputFile = file(filename, 'w')
	outputFile.write(data)
	outputFile.close()

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ There never was a good war or a bad peace.
\__/  Benjamin Franklin




More information about the Python-list mailing list