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.
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
- 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.