Computer >> Computer tutorials >  >> Programming >> Python

How do you append to a file with Python?


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.read()
f.write('Hello World')
f.close()

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