Python – Append content of one text file to another
Having two file names entered from users, the task is to append the content of the second file to the content of the first file.
Example
Input : file1.txt file2.txt Output : Content of first file (before appending) - geeksfor Content of second file (before appending) - geeks Content of first file (after appending) - geeksforgeeks Content of second file (after appending) - geeks
Algorithm :
- Enter the names of the files.
- Open both the files in read only mode using the open() function.
- Print the contents of the files before appending using the read() function.
- Close both the files using the close() function.
- 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.
- Close both the files.
Suppose the text files file1.txt and file2.txt contain the following data.
file1.txt
file2.txt
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 :