Append Content of One File to Another File in Python

This tutorial will show you how to read the content of a file and append it to another file in Python.

 

Python Append Content to File Example

The example below demonstrates opening a file and writing its contents to another file using the Python open() function:

 

with open('/path/to/file.txt', 'r') as f:
   data = f.read()

with open('/path/to/output.txt', 'a') as f:
   f.write(data)

 

The key thing to observe in the example above is to set the mode of the open() function to append ('a'). A write() operation adds the new content to the end of the old content in append mode rather than overwriting it.