How to use writelines to append new lines to an existing file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nico Grubert

    How to use writelines to append new lines to an existing file

    Hi there,

    I would like to open an existing file that contains some lines of text
    in order to append a new line at the end of the content.

    My first try was:
    [color=blue][color=green][color=darkred]
    >>> f = open('/tmp/myfile', 'w') #create new file for writing
    >>> f.writelines('1 23') #write first line
    >>> f.close()
    >>> f = open('/tmp/myfile', 'w') #open existing file to append new line
    >>> f.writelines('4 56')
    >>> f.close()
    >>> f = open('/tmp/myfile', 'r') # open file for reading
    >>> f.read()[/color][/color][/color]
    '456'

    I supposed to have:[color=blue][color=green][color=darkred]
    >>> f.read()[/color][/color][/color]
    '123\n456\n'


    Does f = open('/tmp/myfile', 'w') overwrite the existing file or does
    f.writelines('4 56') replace the first line in the existing file?

    Nico
  • Grigoris Tsolakidis

    #2
    Re: How to use writelines to append new lines to an existing file

    Use
    file = open(open('/tmp/myfile', 'a')) the second time
    when you want to append line

    "Nico Grubert" <nicogrubert@gm ail.com> wrote in message
    news:mailman.78 1.1127393534.50 9.python-list@python.org ...[color=blue]
    > Hi there,
    >
    > I would like to open an existing file that contains some lines of text in
    > order to append a new line at the end of the content.
    >
    > My first try was:
    >[color=green][color=darkred]
    > >>> f = open('/tmp/myfile', 'w') #create new file for writing
    > >>> f.writelines('1 23') #write first line
    > >>> f.close()
    > >>> f = open('/tmp/myfile', 'w') #open existing file to append new line
    > >>> f.writelines('4 56')
    > >>> f.close()
    > >>> f = open('/tmp/myfile', 'r') # open file for reading
    > >>> f.read()[/color][/color]
    > '456'
    >
    > I supposed to have:[color=green][color=darkred]
    > >>> f.read()[/color][/color]
    > '123\n456\n'
    >
    >
    > Does f = open('/tmp/myfile', 'w') overwrite the existing file or does
    > f.writelines('4 56') replace the first line in the existing file?
    >
    > Nico[/color]


    Comment

    • Max Erickson

      #3
      Re: How to use writelines to append new lines to an existing file

      Nico Grubert <nicogrubert@gm ail.com> wrote in
      news:mailman.78 1.1127393534.50 9.python-list@python.org :
      [color=blue]
      > Hi there,
      >
      > I would like to open an existing file that contains some lines of
      > text in order to append a new line at the end of the content.
      >
      > My first try was:
      >[color=green][color=darkred]
      > >>> f = open('/tmp/myfile', 'w') #create new file for writing
      > >>> f.writelines('1 23') #write first line
      > >>> f.close()
      > >>> f = open('/tmp/myfile', 'w') #open existing file to append
      > >>> new line f.writelines('4 56')
      > >>> f.close()
      > >>> f = open('/tmp/myfile', 'r') # open file for reading
      > >>> f.read()[/color][/color]
      > '456'
      >
      > I supposed to have:[color=green][color=darkred]
      > >>> f.read()[/color][/color]
      > '123\n456\n'
      >
      >
      > Does f = open('/tmp/myfile', 'w') overwrite the existing file
      > or does f.writelines('4 56') replace the first line in the
      > existing file?
      >
      > Nico[/color]

      There is a good explanation in the tutorial:


      and more detail is available in the docs for builtin functions:

      (look under file() but probably still use open())

      That said, open(file, 'a') will open an existing file to append.

      max

      Comment

      Working...