This is a Python Program to append the contents of one file to another file.
Problem Description
The program takes the name of the file to be read from and the file to append into from the user and appends the contents of one file to another.
Problem Solution
1. Take the name of the file to be read from and the file to append into from the user.
2. Open the file to be read and read the content of the file and store it in a variable.
3. Open the file to append the data to and write the data stored in the variable into the file.
4. Close both the files.
5. Exit.
Program/Source Code
Here is source code of the Python Program to append the contents of one file to another file. The program output is also shown below.
name1 = input(“Enter file to be read from: “)
name2 = input(“Enter file to be appended to: “)
fin = open(name1, “r”)
data2 = fin.read()
fin.close()
fout = open(name2, “a”)
fout.write(data2)
fout.close()
Runtime Test Cases
Case 1:
Output:
Enter file to be read from: test.txt
Enter file to be appended to: test1.txt
Contents of file test.txt:
Appending!!
Contents of file test1.txt (before appending):
Original
Contents of file test1.txt (after appending):
Original Appending!!
Case 2:
Enter file to be read from: out.txt
Enter file to be appended to: out1.txt
Contents of file test.txt:
world
Contents of file test1.txt (before appending):
Hello
Contents of file test1.txt (after appending):
Hello world