Python - Append content of one text file to another
Last Updated : 02 Aug, 2023
Having two file names entered by users, the task is to append the content of the second file to the content of the first file with Python.
Append the content of one text file to another
Suppose the text files file1.txt and file2.txt contain the following data.
file1.txt

file2.txt

Append the content of one text file to another using the file object
Enter the names of the files then open both files in read-only mode using the open() function and print the contents of the files before appending them using the read() function now close both files using the close() function. and open the first file in append mode and the second file in read mode. Append the contents of the second file to the first file using the write() function. Reposition the cursor of the files at the beginning using the seek() function. Print the contents of the appended files. and Close both files.
python3 # entering the file names firstfile = input("Enter the name of first file ") secondfile = input("Enter the name of second file ") # opening both files in read only mode to read initial contents f1 = open(firstfile, 'r') f2 = open(secondfile, 'r') # printing the contents of the file before appending print('content of first file before appending -', f1.read()) print('content of second file before appending -', f2.read()) # closing the files f1.close() f2.close() # opening first file in append mode and second file in read mode f1 = open(firstfile, 'a+') f2 = open(secondfile, 'r') # appending the contents of the second file to the first file f1.write(f2.read()) # relocating the cursor of the files at the beginning f1.seek(0) f2.seek(0) # printing the contents of the files after appendng print('content of first file after appending -', f1.read()) print('content of second file after appending -', f2.read()) # closing the files f1.close() f2.close()
Output:

Time complexity: O(n), where n is the total number of characters in both files.
Auxiliary space: O(1), since we are not using any additional data structures and are only reading and writing to the files.
Append the content of one text file to another using the using shutil
module
we first open (file1) and after it, we open (file2) we open file one in read mode and file2 in append mode then copyfileobj()
the function reads data from file1 in chunks and appends it to file2 until the entire content is copied.
Python3 import shutil def append_files_method2(file1_path, file2_path): with open(file1_path, 'r') as file1: with open(file2_path, 'a') as file2: shutil.copyfileobj(file1, file2) # Example usage: file1_path = 'file1.txt' file2_path = 'file2.txt' append_files_method2(file1_path, file2_path)
Output:

Time complexity: O(n)
Auxiliary space: O(1), since we are not using any additional data structures and are only reading and writing to the files.
Append the content of one text file to another using the using fileinput
Module
We here first open the target file2 in append mode. Then, by using fileinput.input()
, we open the source file1 in read mode and iterate over its lines. and then for every line in file1, we write it to file2 using the write()
method. This way work perfectly with large file to read and write data from one text file to another
Python3 import fileinput def append_files_method3(file1_path, file2_path): with open(file2_path, 'a') as file2: with fileinput.input(files=file1_path) as file1: for line in file1: file2.write(line) # Example usage: file1_path = 'file1.txt' file2_path = 'file2.txt' append_files_method3(file1_path, file2_path)
Output:

Time complexity: O(n), where n is the total number of characters in both files.
Auxiliary space: O(1)
Similar Reads
Convert Text file to JSON in Python JSON (JavaScript Object Notation) is a data-interchange format that is human-readable text and is used to transmit data, especially between web applications and servers. The JSON files will be like nested dictionaries in Python. To convert a text file into JSON, there is a json module in Python. Thi
4 min read
Python - Copy all the content of one file to another file in uppercase Copying the content of one file to another while transforming the text to uppercase can be easily done using Pythonâs file handling features. By reading the source file, converting its contents to uppercase and writing to a new file, you can achieve this efficiently. Letâs look at the definitions of
3 min read
How to open and close a file in Python There might arise a situation where one needs to interact with external files with Python. Python provides inbuilt functions for creating, writing, and reading files. In this article, we will be discussing how to open an external file and close the same using Python. Opening a file in Python There a
4 min read
Python append to a file While reading or writing to a file, access mode governs the type of operations possible in the opened file. It refers to how the file will be used once it's opened. These modes also define the location of the File Handle in the file. The definition of these access modes is as follows:Append Only (âa
3 min read
Read content from one file and write it into another file Prerequisite: Reading and Writing to text files in Python Python provides inbuilt functions for creating, writing, and reading files. Two types of files can be handled in python, normal text files and binary files (written in binary language,0s, and 1s). Text files: In this type of file, Each line o
2 min read