How to Append Data to an Existing File using Python

To append data to an existing file using Python:

# The new data to be appended
new_data = " This is the additional data to be added to the file"

# File path, including the "r" letter before the path to avoid unicode errors
file_path = r"path to file\file_name.file_extension"

# Opening the file in append mode ("a") and writing the data
with open(file_path, "a") as file:
    file.write(new_data)

Example of Appending Data to an Existing File using Python

(1) Assuming an existing text file called “output“.

The file is currently stored in a folder called “Test“, where the full path of the file is:

C:\Users\Ron\Desktop\Test\output.txt

The text file currently contains the following information:

Dive into the world of coding and data analytics with our cutting-edge tutorials! Uncover the power of Python and R through our engaging tutorials crafted to simplify complex concepts.

The ultimate goal is to append the following new data at the end of the file without erasing its contents:

Join our community of learners by subscribing to our channel and embarking on an exciting educational voyage.

(2) The following Python script can then be used to append the data at the end of the file:

# The new data to be appended
new_data = " Join our community of learners by subscribing to our channel."

# File path, including the "r" letter before the path to avoid unicode errors
file_path = r"C:\Users\Ron\Desktop\Test\output.txt"

# Opening the file in append mode ("a") and writing the data
with open(file_path, "a") as file:
    file.write(new_data)

Notice that there is a space before the first letter of the new_data string. This is to ensure there is a space between the last sentence that is currently in the text file, and the beginning of the new appended sentence.

If you run the script (adjusted to your file_path), you’ll see the following content of the text file after appending the data, where the appended portion is highlighted in green:

Dive into the world of coding and data analytics with our cutting-edge tutorials! Uncover the power of Python and R through our engaging tutorials crafted to simplify complex concepts. Join our community of learners by subscribing to our channel.

(3) What if you want to append the data in a new line?

In that case, simply add “\n” at the beginning of the new data that you wish to add (as highlighted in yellow):

# The new data to be appended
new_data = "\nJoin our community of learners by subscribing to our channel."

# File path, including the "r" letter before the path to avoid unicode errors
file_path = r"C:\Users\Ron\Desktop\Test\output.txt"

# Opening the file in append mode ("a") and writing the data
with open(file_path, "a") as file:
    file.write(new_data)

The resulted text file:

Dive into the world of coding and data analytics with our cutting-edge tutorials!
Uncover the power of Python and R through our engaging tutorials crafted to simplify complex concepts.
Join our community of learners by subscribing to our channel.