[Tutor] Replacing the string in a file

Stefan Behnel stefan_ml at behnel.de
Fri Jan 22 08:37:32 CET 2010


vanam, 22.01.2010 07:44:
> [Query]: How to write Python string to PYTHON to that file without
> intact of the full contents

What one would normally do is: read the file (line by line if that's ok in
your case), replace the word by the new one, write the new line to a new
temporary file. When done, move the new file over the old one. That also
makes sure that your changes are 'atomic' (as good as it gets), i.e. your
original file is still there when you encounter an error during the
replacement run.

Stefan



More information about the Tutor mailing list