FAQ
Hello.

I am a little frustrated. After looking all over the place ( Learning
Python, the Python Pocket Reference, Library Reference) I have been unable
to find how to append to the end of an existing text file using python in
any way other than reading the entire contents of the file out to a list,
adding the additional information, and then writing it all back to the
original file. I know there must be a better way to do this; for the life
of me, however, I seem to me unable to find it.

Can anyone help me out?

-- Lenny Self
lenny at squiggie.com

Search Discussions

  • Remco Gerlich at Mar 14, 2000 at 6:02 pm

    Lenny Self wrote in comp.lang.python:
    Hello.

    I am a little frustrated. After looking all over the place ( Learning
    Python, the Python Pocket Reference, Library Reference) I have been unable
    to find how to append to the end of an existing text file using python in
    any way other than reading the entire contents of the file out to a list,
    adding the additional information, and then writing it all back to the
    original file. I know there must be a better way to do this; for the life
    of me, however, I seem to me unable to find it.

    Can anyone help me out?
    open(filename, 'a')

    'a' for append. I'm sure it's in those books you named as well, but it's also
    in the output of "print open.__doc__".

    --
    Remco Gerlich, scarblac at pino.selwerd.nl

    This is no way to be
    Man ought to be free -- Ted Bundy
    That man should be me
  • Gregoire Welraeds at Mar 16, 2000 at 8:37 am

    In reply to the message of Remco Gerlich sent on Mar 14 (see below) :

    open(filename, 'a')

    'a' for append. I'm sure it's in those books you named as well, but it's also
    in the output of "print open.__doc__".
    It's in the beginning of the library reference... In the section talking
    about the open function.



    --
    Life is not fair
    But the root password helps
    --

    Gregoire Welraeds
    greg at perceval.be
    Perceval Development team
    -------------------------------------------------------------------------------
    Perceval Technologies sa/nv Tel: +32-2-6409194
    Rue Tenbosch, 9 Fax: +32-2-6403154
    B-1000 Brussels general information: info at perceval.net
    BELGIUM technical information: helpdesk at perceval.net
    URL: http://www.perceval.be/
    -------------------------------------------------------------------------------
    On 14 Mar 2000, Remco Gerlich wrote:

    Date: 14 Mar 2000 18:02:24 GMT
    From: Remco Gerlich <scarblac-spamtrap at pino.selwerd.nl>
    Reply-To: scarblac-rt at pino.selwerd.nl
    To: python-list at python.org
    Newsgroups: comp.lang.python
    Subject: Re: appending a file

    Lenny Self wrote in comp.lang.python:
    Hello.

    I am a little frustrated. After looking all over the place ( Learning
    Python, the Python Pocket Reference, Library Reference) I have been unable
    to find how to append to the end of an existing text file using python in
    any way other than reading the entire contents of the file out to a list,
    adding the additional information, and then writing it all back to the
    original file. I know there must be a better way to do this; for the life
    of me, however, I seem to me unable to find it.

    Can anyone help me out?
    open(filename, 'a')

    'a' for append. I'm sure it's in those books you named as well, but it's also
    in the output of "print open.__doc__".

    --
    Remco Gerlich, scarblac at pino.selwerd.nl

    This is no way to be
    Man ought to be free -- Ted Bundy
    That man should be me
    --
    http://www.python.org/mailman/listinfo/python-list
  • Jeff Collins at Mar 14, 2000 at 6:13 pm
    This is most likely a better way than this, but the solution
    popped into my head upon reading your question. If you are
    using Unix, try:

    fh = open('jnkxxx','w')
    fh.write('line 1\n')
    fh.write('line 2\n')
    fh.close()

    # cat jnkxxx
    # line 1
    # line 2

    import os

    fh = os.popen("cat >> jnkxxx", "w")
    fh.write('line 3\n')
    fh.write('line 4\n')
    fh.close()

    # cat jnkxxx
    # line 1
    # line 2
    # line 3
    # line 4


    Jeff


    Lenny Self writes:
    Hello. >
    I am a little frustrated. After looking all over the place ( Learning
    Python, the Python Pocket Reference, Library Reference) I have been unable
    to find how to append to the end of an existing text file using python in
    any way other than reading the entire contents of the file out to a list,
    adding the additional information, and then writing it all back to the
    original file. I know there must be a better way to do this; for the life
    of me, however, I seem to me unable to find it. >
    Can anyone help me out? >
    -- Lenny Self
    lenny at squiggie.com
    >
    >
    >

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 14, '00 at 5:41p
activeMar 16, '00 at 8:37a
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase