How to Append Content to File in Python


In this example, we will learn how to write to an existing file and append content to the file using the open() function in Python.

Source Code

f = open('myfile2.txt', 'a')
f.write("Appends a line to the file.")
f.close()

# Open and read the file after the appending:
f = open('myfile2.txt', 'r')
text = f.read()
print(text) 

Output:

Hello World!
Python is a programming language that lets you work more quickly and integrate your systems more effectively.
I love Python!Appends a line to the file.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments