How to Append Text to File in Python? Examples


Python - Append Text to File

To append text to an existing file in Python, follow these steps:

  1. Open the file in append mode a+.
  2. Write or append the text to the file.
  3. Close the file.

Example 1: Concatenate or Append Text to File

In this example, we have an existing file data.txt with some text. We will append additional text to the file by following the steps above.

Python Program

fin = open("data.txt", "a")

fin.write('\nThis is newly appended text.');

fin.close()

Explanation

  1. Open the file data.txt in append mode a.
  2. Use fin.write() to add new text. The \n ensures that the text is appended on a new line.
  3. Close the file with fin.close() to save the changes.

Input Text File - data.txt before running the Python example:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.

Text File with Appended Text after running the Python example:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
This is newly appended text.

Example 2: Append Text to File in Text Mode

You can handle the file in either text or binary mode. By default, the file is handled in text mode. In this example, we explicitly open the file in text mode by appending "t" to the append mode "a".

Python Program

fin = open("data.txt", "at")

fin.write('\nThis is newly appended text.');

fin.close()

Explanation

  1. Open the file data.txt in append mode at, which explicitly handles it in text mode.
  2. Append text using fin.write(), similar to Example 1.
  3. Close the file after appending the text with fin.close().

Example 3: Append Multiple Lines to File

In some cases, you might want to append multiple lines of text to a file. Below, we append multiple lines to the data.txt file.

Python Program

fin = open("data.txt", "a")

fin.write('\nFirst line appended.\nSecond line appended.');

fin.close()

Explanation

  1. Open the file data.txt in append mode a.
  2. Append multiple lines of text by using \n to create line breaks between the new lines of text.
  3. Close the file with fin.close().

Input Text File - data.txt before running the Python example:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.

Text File with Multiple Lines Appended after running the Python example:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
First line appended.
Second line appended.

Example 4: Append Text to File in Binary Mode

If you need to append data in binary mode (e.g., when dealing with non-text files like images), you can use "ab" mode. In the example below, we append binary data to the file.

Python Program

fin = open("data.bin", "ab")

fin.write(b'\x89PNG\x0D\x0A');

fin.close()

Explanation

  1. Open the binary file data.bin in append binary mode ab.
  2. Use fin.write() to append binary data. The b'' syntax indicates binary data.
  3. Close the binary file with fin.close().

Summary

In this tutorial of Python Examples, we explored how to append text to a file in Python. We demonstrated appending text in text and binary modes, appending a single line or multiple lines, and overwriting the file in various use cases.


Python Libraries