Kodeclik Blog
ABOUT
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
PROGRAMS
Computer Science Camps
Online CS Classes
Math Camps
Online Math Classes
Self Learning CS Courses
Coding Contest
POPULAR CLASSES
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2024. All rights reserved.
You will sometimes have a situation where you have two or more files containing data and you desire to merge them into a single file. Here we will learn a Python program do accomplish this task.
Let us suppose we have 3 files named file1.txt, file2.txt, and file3.txt containing:
The goal is to merge them into a single file as shown below:
Let us write a Python program to first read these files. We embed the file opening and reading commands inside a for loop to cycle through these multiple files:
Here the loop cycles through the three files in the list called “myfiles”. For each of these files, represented by the variable “f”, we open the file and use “infile” as the file pointer variable. We then use the read() method to read the contents of “infile” and print them. Note that depending on the loop iteration, the variable “infile” refers to different files.
The output will be, as expected:
Now we are ready to write these contents into a new file. Let us call it “mergedfile.txt”. The updated program will be:
Note that we now embed the existing for loop inside a larger loop that opens “mergedfile.txt” in write mode and then instead of printing the contents to the standard output channel, we write them into the newfile.
If we run this program, you should see a new file called “mergedfile.txt” being created by the program. Let us open it and inspect its contents:
Oops! Looks like we didn’t print a newline after each file’s contents. Here is an updated program:
The new output will be as we expected:
You can extend this program to work on more files by simply updating the “myfiles” variable.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.
<file1.txt>
Harry Potter
and the Philosopher's Stone
<file2.txt>
Harry Potter
and the Chamber of Secrets
<file3.txt>
Harry Potter
and the Prisoner of Azkaban
myfiles = ['file1.txt','file2.txt','file3.txt']
for f in myfiles:
with open(f) as infile:
contents = infile.read()
print(contents)
Harry Potter
and the Philosopher's Stone
Harry Potter
and the Chamber of Secrets
Harry Potter
and the Prisoner of Azkaban
myfiles = ['file1.txt','file2.txt','file3.txt']
with open('mergedfile.txt', 'w') as newfile:
for f in myfiles:
with open(f) as infile:
contents = infile.read()
newfile.write(contents)
Harry Potter
and the Philosopher's StoneHarry Potter
and the Chamber of SecretsHarry Potter
and the Prisoner of Azkaban
myfiles = ['file1.txt','file2.txt','file3.txt']
with open('mergedfile.txt', 'w') as newfile:
for f in myfiles:
with open(f) as infile:
contents = infile.read()
newfile.write(contents)
newfile.write('\n')
Harry Potter
and the Philosopher's Stone
Harry Potter
and the Chamber of Secrets
Harry Potter
and the Prisoner of Azkaban