How do you append to a file with Python?

PythonServer Side ProgrammingProgramming

To append to a file, you must open the file in append mode by specifying 'a' as the mode(a=append) when opening it. For example,

f = open('my_file.txt', 'a')
file_content =
f.write('Hello World')

Above code opens my_file.txt in append mode and appends the file to contain "Hello World" at the end.

Updated on 01-Oct-2019 11:48:01