codescracker


python

Python Program to Merge Two Files



« Previous Program Next Program »


Merge Two Files in Python

To merge two files in python, you have to ask from user to enter name of the first and second file, and then ask a file name to create a file to place the merged content of the two file into this newly created file.

To merge the content of first and second file and put all the merged content from first and second file into the third file, you have to open the first and second file to read content of both the file, and then finally store the merged content of both files into another third file to successfully perform the merging of two files into another file.

Python Programming Code to Merge Two Files

Following python program ask from user to enter first and second file name, then to create a file to enter or place the merged content of first and second file into this file as shown here in this program:

# Python Program - Merge Two Files

import shutil;
print("Enter 'x' for exit.");
filename1 = input("Enter first file name to merge: ");
if filename1 == 'x':
    exit();
else:
    filename2 = input("Enter second file name to merge: ");
    filename3 = input("Create a new file to merge content of two file inside this file: ");
    print();
    print("Merging the content of two file in",filename3);
    with open(filename3, "wb") as wfd:
        for f in [filename1, filename2]:
            with open(f, "rb") as fd:
                shutil.copyfileobj(fd, wfd, 1024*1024*10);
    print("\nContent merged successfully.!");
    print("Want to see ? (y/n): ");
    check = input();
    if check == 'n':
        exit();
    else:
        print();
        c = open(filename3, "r");
        print(c.read());
        c.close();

Let's suppose we have two files present inside the current directory namely codes.txt and cracker.txt. The file codes.txt contains following contents:

I am a sentence.
I am in a file named codes.txt.
I am stored in python current directory.

And the file cracker.txt contains the following contents:

This is sentence.
This is in cracker.txt.
This is in python current directory.

Now let's merge both the file into another new file named codescracker.txt as shown in below sample outputs:

Here is the sample run of the above python program to illustrates how to merge two files to store the content of these files into another file.

python merge two files

Now supply first file name say codes.txt and press enter key, and then supply second file name say cracker.txt and press enter key, and now supply or enter a file name to merge the content of these two file into it say codescracker.txt and press enter key to finally perform merge operation as shown in given sample run here:

merge two files in python

Now supply y and press enter key to see the merged content present inside the file codescracker.txt as shown here:

python program merge two files

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test