# Python Program to Merge Two Files into a Third File

Files in Python:

Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).

We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).

We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.

As a result, a file operation in Python is performed in the following order:

• Create a new file
• You can either read or write (perform the operations)
• Close the file.

Given a file, the task is to merge given two files into a third file in Python

## Program to Merge Two Files into a Third File in Python

Below is the full approach for merging given two files into a third file in Python

### Method #1: Using Concatenation

Approach:

• Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
• Open the first file in read-only mode. In this case, we’re simply reading the contents of the file.
• Read the above file using the read() function(get the content) and store it in a variable.
• Make another variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
• Open the second file in read-only mode. In this case, we’re simply reading the contents of the file.
• Read the above file using the read() function(get the content) and store it in a variable.
• Add next line to the above first file.
• Add or concatenate both the first and the second file’s data and store it into a first file
• Make another variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
• Open the merged file in write mode. In this case, we’re writing the contents into the file.
• Write the data of the first file(concatenation of both first and second files) into the merged file using the write() function.
• Close the merged file using the close() function.
• The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
gvn_fstfile = "samplefile_1.txt"
# Open the first file in read-only mode. In this case, we're simply reading the contents of the file.
with open(gvn_fstfile, 'r') as givenfilecontent:
# Read the above file using the read() function(get the content) and store it in a variable

# Make another variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
gvn_scndfile = "samplefile_2.txt"
# Open the second file in read-only mode. In this case, we're simply reading the contents of the file.
with open(gvn_scndfile, 'r') as givenfilecontent:
# Read the above file using the read() function(get the content) and store it in a variable

# Add nextline to the above first file.
fst_file += "\n"
# Add or concatenate both the first and the second files data and store it into a first file
fst_file += scnd_file
# Make another variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
mergedFile = 'mergedfile.txt'
# Open the merged file in write mode. In this case, we're writing the contents into the file.
with open(mergedFile, 'w') as merged_File:
# Write the data of the firstfile(concatenation of both first and second files)
# into the merged file using the write() function.
merged_File.write(fst_file)
# Close the merged file using the close() function
merged_File.close()

Output:

hello this is btechgeeks
good morning btechgeeks

File Content (samplefile_1.txt):

hello this is btechgeeks

File Content (samplefile_2.txt):

good morning btechgeeks

File Content (mergedfile.txt):

hello this is btechgeeks
good morning btechgeeks

### Method #2: Using For Loop

Approach:

• Pass both the first and second files as a list and store it in a variable.
• Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
• Open the merged file in write mode. In this case, we’re writing the contents into the file.
• Iterate in the above-read files list using the for loop.
• Open the file using the open and with function.
• Write the content of the file to the merged file using the write() function.
• Add a new line character to the merged file to differentiate both files.
• The Exit of Program.

Below is the implementation:

# Pass both the first and second files as a list and store it in a variable
# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
mergedFile = 'mergedfile.txt'
# Open the merged file in write mode. In this case, we're writing the contents into the file.
with open(mergedFile, 'w') as merged_File:
# Iterate in the above read files list using the for loop.
# Open the file using the open and with function
with open(file) as inputFile:
# Write the content of the file to the merged file using the write() function
merged_File.write("\n")
hello this is btechgeeks
good morning btechgeeks