How do I append to a file?

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.

Leave a Comment