Python Program to Merge Two Files

To merge the content of two files into a third file using a Python program. Then, first of all, we need to create two files in the current directory, the directory where the Python source code is saved.

Therefore, let's create two files namely one.txt and two.txt with some contents. And a third file named three.txt with or without any content. Here is the snapshot of the current directory, along with all these three files, opened:

python merge two files

Now let's create the program, to merge the content of two text files into the third. It is not compulsory to create the third file, because while writing the content using w mode, the file automatically gets created, if not available.

Merge Two Files into Third File in Python

The question is, write a Python program to merge the content of two files into a third file. The name of all the three files must be received by user at run-time of the program. Answer to this question, is the program given below:

print("Enter the Name of First File: ", end="")
fileOne = input()
print("Enter the Name of Second File: ", end="")
fileTwo = input()
print("Enter the Name of Third File: ", end="")
fileThree = input()

content = ""
fh = open(fileOne, "r")
for line in fh:
    content = content + line + '\n'
fh.close()

fh = open(fileTwo, "r")
for line in fh:
    content = content + line + '\n'
fh.close()

fh = open(fileThree, "w")
fh.write(content)

print("\nFile merged successfully!")

The snapshot given below shows the sample run of above program, with user input one.txt and two.txt as name of first and second file whose content is going to merge into the third file named three.txt, also entered by user as name of third file:

merge two files into third python

Here is the new snapshot of the three files, after executing the above program, using the sample run shown in the snapshot:

python program merge two files

Note - The end parameter used in above program, in a way to skip an automatic insertion of a newline.

Note - Use a in place of w mode, so that the previous content of third file does not gets deleted or overwritten.

Now the question is, what if user enters the name of file that does not available in the directory ?
therefore in that case, wrap the open() method, that opens a file, into try block, so that we can catch the exception named FileNotFoundError.

Here is the modified version of above program, that handles with file names, that does not available in the current directory:

print("Enter the Name of First File: ", end="")
fileOne = input()
try:
    fhOne = open(fileOne, "r")
    print("Enter the Name of Second File: ", end="")
    fileTwo = input()
    try:
        fhTwo = open(fileTwo, "r")
        print("Enter the Name of Third File: ", end="")
        fileThree = input()

        content = ""
        for line in fhOne:
            content = content + line + '\n'
        for line in fhTwo:
            content = content + line + '\n'
        fhOne.close()
        fhTwo.close()

        fh = open(fileThree, "w")
        fh.write(content)
        print("\nFile merged successfully!")

    except FileNotFoundError:
        print("\nFile not found!")
except FileNotFoundError:
    print("\nFile not found!")

To learn about File Handling in detail, refer to its separate tutorial.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »