Python Program to Merge Two Files into a Third File

Last Updated : 15 Jan, 2026

Merging files is a common file-handling task in Python. In this article, we’ll see how to merge two files into a third file using multiple approaches.

Note: Make sure file1.txt and file2.txt exist in the same directory as your Python script.

Sample textfile:

file1.txt

Python-file-handling-file1 file2.txt Python-file-handling-file2

Using shutil.copyfileobj()

shutil.copyfileobj() efficiently copies content from one file object to another, without loading the entire file into memory. Ideal for large files.

Python
with open('merged_file.txt', 'w') as outfile:
    for filename in ['file1.txt', 'file2.txt']:
        with open(filename, 'r') as infile:
            outfile.write(infile.read())  
            outfile.write('\n')         

Output (merged_file.txt)

This is the content from file1 .
Hello There.
This is in file1
Hello Geeks
This is in file2

Explanation:

  • with open('merged_file.txt', 'w') as outfile: Opens the output file in binary write mode.
  • for filename in ['file1.txt', 'file2.txt']: Iterates over the files to be merged.
  • outfile.write(infile.read()): Writes content as-is to the output file.

Using the os Module

This method uses basic file handling with the os module to read files line by line and write their contents into a single file. It is memory-efficient and suitable for large files.

Python
with open('merged_file.txt', 'w') as outfile:
    for filename in ['file1.txt', 'file2.txt']:
        with open(filename, 'r') as infile:
            content = infile.read()
            outfile.write(content)
            if not content.endswith('\n'):
                outfile.write('\n')

Output

This is the content from file1 .
Hello There.
This is in file1
Hello Geeks
This is in file2

Explanation:

  • for filename in ['file1.txt', 'file2.txt']: Iterates over the input files to be merged.
  • content = infile.read(): Read entire content of the input file.
  • if not content.endswith('\n'): Check if last line ends with newline.
  • outfile.write('\n'): Add newline only if missing, so next file starts on a new line.

Using a For Loop

This method merges files by looping through a list of filenames and writing their contents to a new file. It is simple, readable, and beginner-friendly, making it suitable for small to medium-sized files.

Python
filenames = ['file1.txt', 'file2.txt']

with open('file3.txt', 'w') as outfile:
    for name in filenames:
        with open(name) as infile:
            outfile.write(infile.read())
        outfile.write("\n") 

Output

This is the content from file1.
Hello There.
This is in file1
Hello Geeks
This is in file2

Explanation:

  • filenames = ['file1.txt', 'file2.txt']: Stores the names of the files to be merged.
  • for name in filenames: Loops through each input file.
  • with open(name) as infile: Opens each file in read mode.
  • outfile.write(infile.read()): Reads the entire file content and writes it to file3.txt.

Naive Approach

This method reads the entire contents of both files into strings, concatenates them, and writes the combined string to a new file.

Python
with open('file1.txt') as fp:
    data1 = fp.read()

with open('file2.txt') as fp:
    data2 = fp.read()

with open('file3.txt', 'w') as fp:
    fp.write(data1 + "\n" + data2)

Output

This is the content from file1.
Hello There.
This is in file1
Hello Geeks
This is in file2

Explanation:

  • data1 = fp.read(): Reads the entire content of file1.txt into the string data1.
  • with open('file2.txt') as fp: Opens file2.txt in read mode.
  • data2 = fp.read(): Reads the entire content of file2.txt into the string data2.
Comment

Explore