By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Writing at the beginning of a file

P: n/a
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

Sep 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Sep 14 '05 #2

P: n/a
On 2005-09-14, Thierry Lam <la********@gmail.com> wrote:
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.

--
Grant Edwards grante Yow! Are you selling NYLON
at OIL WELLS?? If so, we can
visi.com use TWO DOZEN!!
Sep 14 '05 #3

P: n/a
On 14 Sep 2005 07:13:50 -0700, "Thierry Lam" <la********@gmail.com> wrote:
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
Sep 15 '05 #4

P: n/a

"Thierry Lam" <la********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Sep 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.