Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Writing at the beginning of a file

Reply
Thread Tools

Writing at the beginning of a file

 
 
Thierry Lam
Guest
Posts: n/a
 
      09-14-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

 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      09-14-2005
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

 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      09-14-2005
On 2005-09-14, Thierry Lam <(E-Mail Removed)> 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!!
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      09-15-2005
On 14 Sep 2005 07:13:50 -0700, "Thierry Lam" <(E-Mail Removed)> 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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-15-2005

"Thierry Lam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
how to rewind to file beginning immediately after writing... Rajorshi Biswas C Programming 3 07-20-2006 01:03 PM
Writing big XML files where beginning depends on end. Magnus Lycka Python 10 11-29-2005 03:05 AM
problem: writing out an XML file always includes the commented DTD in the beginning. Why? bronski Java 0 02-16-2005 11:43 AM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM



Advertisments