[Tutor] writing changes to a file

alan.gauld@bt.com alan.gauld@bt.com
Tue, 15 Feb 2000 12:44:55 -0000

> > file, and then save the new version. Is it possible to open 
> a file to write some changes to it in one go?

Yes, under certain circumstances.
In particular if X and Y are the exact same size in bytes you 
can replace in situ by opening the file with "r+" mode - read 
and write.

You can then search the file by reading one char at a time 
and you can then use the seek()/tell() function to find 
your position in the file and write() to put the new data there.

This is frought with danger and I don't recommend it unless 
there's a very good reason for not using an input/output 
file combination. (In fact I've only ever done it in C never 
in Python which has a slightly different write() mechanism... 
- it specifies how many bytwes to write)

But it should be possible...
Its much easier to replace the whole line (aka record) at a 
time but that's usually only practical if the records are 
fixed length.