Categories
append file python

How do I append to a file?

1925

How do I append to a file instead of overwriting it?

0

    2888

    Set the mode in open() to "a" (append) instead of "w" (write):

    with open("test.txt", "a") as myfile:
        myfile.write("appended text")
    

    The documentation lists all the available modes.

    6

    • 13

      This from the tutorial may be useful as well.

      – Dan

      Jul 29, 2011 at 22:44


    • 90

      bluewoodtree: The benefits are similar to that of RAII in C++. If you forget close(), it might take a while before the file is actually closed. It is easier that you might think to forget it when the code has multiple exit points, exceptions and so on.

      – Petter

      Jun 13, 2013 at 17:37

    • 11

      There is a functional difference besides just remembering to close. with opens a context manager which will close the file even if there is an error between opening and close().

      – Brian

      Jul 23, 2020 at 18:49


    • One could easy do with open("test.txt") as myfile: myfile.write("appended text",'a'), but a is needed in open.

      – Timo

      Jan 25, 2021 at 17:54

    • @Timo TypeError: TextIOWrapper.write() takes exactly one argument (2 given)

      Jul 6, 2021 at 9:48

    250

    You need to open the file in append mode, by setting “a” or “ab” as the mode. See open().

    When you open with “a” mode, the write position will always be at the end of the file (an append). You can open with “a+” to allow reading, seek backwards and read (but all writes will still be at the end of the file!).

    Example:

    >>> with open('test1','wb') as f:
            f.write('test')
    >>> with open('test1','ab') as f:
            f.write('koko')
    >>> with open('test1','rb') as f:
            f.read()
    'testkoko'
    

    Note: Using ‘a’ is not the same as opening with ‘w’ and seeking to the end of the file – consider what might happen if another program opened the file and started writing between the seek and the write. On some operating systems, opening the file with ‘a’ guarantees that all your following writes will be appended atomically to the end of the file (even as the file grows by other writes).


    A few more details about how the “a” mode operates (tested on Linux only). Even if you seek back, every write will append to the end of the file:

    >>> f = open('test','a+') # Not using 'with' just to simplify the example REPL session
    >>> f.write('hi')
    >>> f.seek(0)
    >>> f.read()
    'hi'
    >>> f.seek(0)
    >>> f.write('bye') # Will still append despite the seek(0)!
    >>> f.seek(0)
    >>> f.read()
    'hibye'
    

    In fact, the fopen manpage states:

    Opening a file in append mode (a as the first character of mode)
    causes all subsequent write operations to this stream to occur at
    end-of-file, as if preceded the call:

    fseek(stream, 0, SEEK_END);
    

    Old simplified answer (not using with):

    Example: (in a real program use with to close the file – see the documentation)

    >>> open("test","wb").write("test")
    >>> open("test","a+b").write("koko")
    >>> open("test","rb").read()
    'testkoko'
    

    1

    • So this implies, that multiple handles can be held across multiple processes, without any write conflicts?

      Oct 21, 2021 at 18:13

    57

    I always do this,

    f = open('filename.txt', 'a')
    f.write("stuff")
    f.close()
    

    It’s simple, but very useful.

    1

    • 20

      its a little nicer and a little bit safer to write: with open(‘filename’,’a’) as f: f.write(‘stuff’)

      Feb 2, 2015 at 22:00