Open a File in Append Mode in Python
To open a file in append mode in Python, use the built-in open()
function with the mode 'a'
or 'a+'
. The 'a'
mode allows appending data to the file without overwriting existing content, while 'a+'
allows both appending and reading. Below are different ways to open and append to a file in Python.
Examples for Opening a File in Append Mode
1. Appending a Single Line to a File
In this example, we open an existing file and append a new line to it.
In this example, we will consider a file example.txt, open it, and append a single line to this file.

main.py
# Open the file in append mode
file = open("example.txt", "a")
# Append a new line to the file
file.write("\nThis is a new line added to the file.")
# Close the file
file.close()
Explanation:
open("example.txt", "a")
: Opens the file namedexample.txt
in append mode.file.write("This is a new line added to the file.\n")
: Appends the given text at the end of the file.file.close()
: Closes the file to save changes.
Output (File Content After Execution):

2. Appending Multiple Lines Using a Loop
We can append multiple lines efficiently using a loop.

main.py
# Open the file in append mode
with open("example.txt", "a") as file:
# List of lines to append
lines = ["First new line\n", "Second new line\n", "Third new line\n"]
# Writing multiple lines using a loop
for line in lines:
file.write(line)
Explanation:
with open("example.txt", "a") as file:
Opens the file in append mode and ensures it closes automatically.lines = ["First new line\n", "Second new line\n", "Third new line\n"]
: A list of new lines to append.for line in lines: file.write(line)
: Iterates through the list and appends each line.
Output (File Content After Execution):

3. Appending User Input to a File
We can take user input and append it to a file dynamically.

main.py
# Open the file in append mode
with open("example.txt", "a") as file:
# Taking user input
new_text = input("Enter text to append: ") + "\n"
# Appending user input to the file
file.write(new_text)
Explanation:
input("Enter text to append: ")
: Takes user input.file.write(new_text)
: Appends the user input as a new line.
Output (Example User Input and File Content After Execution):


4. Appending to a File and Reading the Updated Content
We can append data and immediately read the updated file content using the 'a+'
mode.
main.py
# Open the file in append and read mode
with open("example.txt", "a+") as file:
# Appending new content
file.write("Appending and reading together.\n")
# Moving cursor to the beginning
file.seek(0)
# Reading and printing updated content
print("Updated File Content:\n", file.read())
Explanation:
open("example.txt", "a+")
: Opens the file in append and read mode.file.write("Appending and reading together.\n")
: Appends a new line.file.seek(0)
: Moves cursor to the beginning of the file.file.read()
: Reads and prints the entire file content.
Output (File Content After Execution):


Conclusion
Appending to a file in Python can be done using different methods:
open("file.txt", "a")
: Opens the file in append mode.write()
: Adds content to the end of the file.- Looping through a list: Adds multiple lines dynamically.
"a+"
mode: Allows both appending and reading.
Using with open()
is recommended for file handling as it ensures proper closing of the file.