Writing at the beginning of a file

T

Thierry Lam

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
 
P

Paul McGuire

Thierry -

Check out the StringIO module. It will allow you to buffer the whole
file into a string, and then give you a pseudo file pointer to the
string buffer, so that your "fp.write"s will work unchanged.

-- Paul
 
G

Grant Edwards

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?
No.

[...]

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.

There isn't.
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?

Looks like you've figured it out already.
 
B

Bengt Richter

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?
Maybe avoid writing at the beginning?

Consider whether appending suitably tagged info at the end of the
file might work for your actual application. If the file-using program
knows of this structure, it can seek e.g. to the end minus a fixed 4 bytes
and read those, assuming that will an n-digit (or maybe rfind-delimited) ascii
number offset to seek back to the beginning of the variable-length chunk
you appended. Then what you read forward from there can be used as if
prefixed to the whole file (or contain fixup info for other parts of
the file before use). This can be a handy way of incrementally modifying
a file without rewriting the whole. It all depends, but it may be an option ;-)

For real safety though, you don't modify or delete an existing important
file until you know you have a new representation safely completed. I say
"new representation" since that also covers the possiblity of original plus
diff patch file as separate files, which could also be an option.

Regards,
Bengt Richter
 
T

Terry Reedy

Thierry Lam said:
Is there an easy way to write something of variable length at the top
of the file? ....
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.

Grant has already given you the two no's to the questions you asked.
Any suggestions please?

A slightly different question "How do I write a variable length file
summary after writing the data so that the summary is easy to read without
reading the entire file, and so I can add data later?" has a yes answer:

1. reserve a fixed amount of space at the top for a 'pointer' to the
summary.
2. write the data
3. write the summary
4. rewind (seek to beginning) and write the 'pointer'.

The pointer can be either the offset from the beginning or the offset from
the end (= length of summary) in either binary or text format.

To read, read the pointer, seek to appropriate place, and read summary.

To add data:
1. read the summary
2. write more data, starting where the old summary started
3. write revised summary and pointer.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top