Writing at the beginning of a file

Thierry Lam lamthierry at gmail.com
Wed Sep 14 10:13:50 EDT 2005


Let's say I already wrote a file and have the following:

testing
testing testing
testing testing testing

Is there an easy way to write something of variable length at the top
of the file?

For example,

6 testing written
testing
testing testing
testing testing testing

I tried to write some garbage on top right after opening the file and
then use seek to overwrite the garbage, but since the string to be
written can be of variable length, I'm not sure how much garbage I have
to write initially.

The other way to do what I want is to write the whole thing to a new
file, but I want to skip that method if there's an alternative way.

Another way of doing it is to buffer the whole file writing into some
variable, but that means I have to change 2000+ lines of codes and
change fp.write() to something else.

Any suggestions please?

Thanks
Thierry




More information about the Python-list mailing list