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

</>
Copy
# 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:

  1. open("example.txt", "a"): Opens the file named example.txt in append mode.
  2. file.write("This is a new line added to the file.\n"): Appends the given text at the end of the file.
  3. 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

</>
Copy
# 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:

  1. with open("example.txt", "a") as file: Opens the file in append mode and ensures it closes automatically.
  2. lines = ["First new line\n", "Second new line\n", "Third new line\n"]: A list of new lines to append.
  3. 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

</>
Copy
# 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:

  1. input("Enter text to append: "): Takes user input.
  2. 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

</>
Copy
# 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:

  1. open("example.txt", "a+"): Opens the file in append and read mode.
  2. file.write("Appending and reading together.\n"): Appends a new line.
  3. file.seek(0): Moves cursor to the beginning of the file.
  4. 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:

  1. open("file.txt", "a"): Opens the file in append mode.
  2. write(): Adds content to the end of the file.
  3. Looping through a list: Adds multiple lines dynamically.
  4. "a+" mode: Allows both appending and reading.

Using with open() is recommended for file handling as it ensures proper closing of the file.