[Tutor] Search and Replace

alan.gauld@bt.com alan.gauld@bt.com
Thu, 28 Jun 2001 16:23:40 +0100


>                 if changed:
>                         print 'Updating :', filename
>                         f = open(filename, 'w')
>                         f.writelines(new_lines)
> 
> This, once again, truncates the file and then writes it out.  I am 
> worried about possible data loss;  

The normal way to deal with this is to create a backup copy of the 
original file then write the new one to a temporary file, then if 
successful delete the original, rename the temporary to the original 
and then only if all that works delete the backup. Thats pretty 
bulletproof if you are worried about data loss.

Alan G.