...

/

Solution: Append One File's Contents to the End of Another

Solution: Append One File's Contents to the End of Another

Learn how to append the contents of one file to the end of another.

We'll cover the following...

The solution to the problem of appending the contents of one file to the end of another is given below.

Solution

Press + to interact
main.py
file2.txt
file1.txt
# Append files
f1 = open('file1.txt', 'r')
para1 = ''
while True :
data = f1.readline( )
if data == '' :
break
para1 += data
f2 = open('file2.txt', 'r+')
para2 = ''
while True :
data = f2.readline( )
if data == '' :
break
para2 += data
para2 += para1
f2.seek(0, 0)
f2.write(para2)
f1.close( )
f2.close( )
f2 = open('file2.txt', 'r')
para2 = ''
while True :
data = f2.readline( )
if data == '' :
break
para2 += data
print(para2)
f2.close( )

Explanation

...