Writing at the beginning of a file

Discussion in 'Python' started by Thierry Lam, Sep 14, 2005.

  1. Thierry Lam

    Thierry Lam Guest

    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
     
    Thierry Lam, Sep 14, 2005
    #1
    1. Advertising

  2. Thierry Lam

    Paul McGuire Guest

    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
     
    Paul McGuire, Sep 14, 2005
    #2
    1. Advertising

  3. On 2005-09-14, Thierry Lam <> 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!!
     
    Grant Edwards, Sep 14, 2005
    #3
  4. On 14 Sep 2005 07:13:50 -0700, "Thierry Lam" <> 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
     
    Bengt Richter, Sep 15, 2005
    #4
  5. Thierry Lam

    Terry Reedy Guest

    "Thierry Lam" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Sep 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,413
    HNguyen
    Dec 21, 2004
  2. bronski
    Replies:
    0
    Views:
    455
    bronski
    Feb 16, 2005
  3. Magnus Lycka
    Replies:
    10
    Views:
    486
  4. Rajorshi  Biswas
    Replies:
    3
    Views:
    373
    Rajorshi Biswas
    Jul 20, 2006
  5. Jesse B.
    Replies:
    9
    Views:
    237
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page