View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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
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 :
To append means to add on, usually to the end of something. You can also use append to mean to fix onto or to attach usually at the end. Sometimes you can change the meaning of a word by removing the suffix and appending another to it. This 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.and finally appends the content of one file to another file in this context.
STEP 1: Take the name of the file to be read from and the file to be appended into the user
STEP 2: Open the file to be read and read the content of the file and store it in a variable. The file to be read is opened using open[] function in the read mode.
STEP 3: Open the file to append the data to and write the data stored in the variable into the file. The file to append the data is opened in the append mode and the data stored in the variable is written into the file.
STEP 4: Close both the files and exit from the program.
name1 = input["Enter file to be read from: "] name2 = input["Enter file to be appended to: "] file = open[name1, "r"] data2 = file.read[] file.close[] fout = open[name2, "a"] fout.write[data2] fout.close[]
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!!