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
firstfile = input ( "Enter the name of first file " )
secondfile = input ( "Enter the name of second file " )
f1 = open (firstfile, 'r' )
f2 = open (secondfile, 'r' )
print ( 'content of first file before appending -' , f1.read())
print ( 'content of second file before appending -' , f2.read())
f1.close()
f2.close()
f1 = open (firstfile, 'a+' )
f2 = open (secondfile, 'r' )
f1.write(f2.read())
f1.seek( 0 )
f2.seek( 0 )
print ( 'content of first file after appending -' , f1.read())
print ( 'content of second file after appending -' , f2.read())
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)
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)
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)