How to Append to a File in Python

If you want to add text to an existing file without deleting what is already there, use append mode.

In Python, append mode is "a" in the open() function. It writes new content at the end of the file instead of replacing the old content.

Quick answer

with open("notes.txt", "a") as file:
    file.write("New line of text\n")

Open the file with mode "a" to append. This adds text at the end instead of replacing the file.

What appending means

Appending means:

  • Adding new content to the end of a file
  • Keeping the existing content unchanged
  • Using file mode "a" when opening the file

This is useful for things like:

  • Notes files
  • Log files
  • Saving results from repeated runs of a program

If you are new to file handling, see Python file handling basics: read and write.

Basic way to append text

The simplest way is:

  1. Open the file with open(filename, "a")
  2. Use a with statement so the file closes automatically
  3. Call write() to add text

Example:

with open("notes.txt", "a") as file:
    file.write("Hello\n")

What this code does

  • open("notes.txt", "a") opens notes.txt in append mode
  • with ... as file: makes sure Python closes the file when done
  • file.write("Hello\n") adds text at the end of the file

If notes.txt already contains:

First line

after running the code, it becomes:

First line
Hello

If you want a broader explanation of open(), read Python open() function explained.

Append a new line correctly

A common beginner mistake is forgetting the newline character \n.

write() does not add a new line automatically.

Example without \n

with open("notes.txt", "a") as file:
    file.write("Another line")

If the file already ends with:

First line

the result might be:

First lineAnother line

Example with \n

with open("notes.txt", "a") as file:
    file.write("Another line\n")

This makes the new text start on its own line.

Helpful tip

Use \n when you want each piece of text to appear as a separate line.

Create the file if it does not exist

Append mode has a helpful behavior:

  • If the file exists, Python adds text to the end
  • If the file does not exist, Python creates it automatically

Example:

with open("new_notes.txt", "a") as file:
    file.write("This file will be created\n")

If new_notes.txt is missing, Python creates it and writes the text.

This is different from reading mode. If you open a missing file with "r", Python raises a FileNotFoundError. For more on that, see how to read a file in Python and FileNotFoundError: Errno 2 No such file or directory.

Append multiple lines

You can append more than one line in a few ways.

Call write() multiple times

with open("notes.txt", "a") as file:
    file.write("Line 1\n")
    file.write("Line 2\n")
    file.write("Line 3\n")

Loop through a list of strings

lines = ["Apple", "Banana", "Cherry"]

with open("notes.txt", "a") as file:
    for line in lines:
        file.write(line + "\n")

This adds each item on a separate line.

Expected file content:

Apple
Banana
Cherry

Make sure each line ends with \n if you want line breaks.

When to use append instead of write

Use:

  • "a" when you want to keep old content and add new content at the end
  • "w" when you want to replace the file's old content

Append mode example

with open("notes.txt", "a") as file:
    file.write("New item\n")

This keeps everything already in the file.

Write mode example

with open("notes.txt", "w") as file:
    file.write("Only this text remains\n")

This clears the old file content before writing.

So if you accidentally use "w" instead of "a", you may delete the file's previous contents.

If you want a separate guide for replacing file contents, see how to write to a file in Python.

Common problems when appending

Here are the most common issues beginners run into.

Forgetting the newline character

Problem:

with open("notes.txt", "a") as file:
    file.write("New text")

This may place the new text directly after the old text.

Fix:

with open("notes.txt", "a") as file:
    file.write("New text\n")

Using "w" by mistake

Problem:

with open("notes.txt", "w") as file:
    file.write("New text\n")

This replaces the old file content.

Fix:

with open("notes.txt", "a") as file:
    file.write("New text\n")

Writing a number instead of a string

file.write() expects a string.

Problem:

with open("notes.txt", "a") as file:
    file.write(123)

This raises a TypeError.

Fix:

with open("notes.txt", "a") as file:
    file.write(str(123) + "\n")

Using the wrong file path

If Python cannot find the folder, the file may not be created where you expect.

Example:

with open("missing_folder/notes.txt", "a") as file:
    file.write("Hello\n")

If missing_folder does not exist, this will fail.

Fix:

  • Check your current working folder
  • Make sure the folder exists
  • Use the correct path

For help with paths, see working with file paths in Python.

Getting a PermissionError

Sometimes the file exists, but Python is not allowed to change it.

This can happen when:

  • The file is read-only
  • The folder has restricted permissions
  • Another system rule blocks access

If that happens, see PermissionError: Errno 13 Permission denied.

Debugging tips

If appending is not working as expected, these quick checks can help.

Check that the file opens

print(open("notes.txt", "a"))

This shows that Python can create a file object for append mode.

Check the exact text being written

print("Line to write:", repr("New line of text\n"))

repr() helps you see special characters like \n.

Check whether the file exists

import os
print(os.path.exists("notes.txt"))

This returns True if the file is present.

Check the current working directory

import os
print(os.getcwd())

This helps you find where Python is looking for the file.

FAQ

Does append mode create a file if it does not exist?

Yes. Opening a file with mode "a" creates the file if Python cannot find it.

Does write() add a new line automatically?

No. You must add \n yourself if you want the next text on a new line.

What is the difference between "a" and "w" in open()?

"a" adds to the end of the file. "w" clears the file and writes new content from the start.

Can I append numbers to a file?

Yes, but convert them to strings first with str(). file.write() expects a string.

See also