Skip to main content

How to merge multiple files into a single file in Python

How to merge multiple files into a single file in Python.

Here's a detailed step-by-step tutorial on how to merge multiple files into a single file in Python:

Step 1: Import the necessary modules To merge multiple files into a single file, we'll need to use the os module for file handling and the shutil module for file operations. Let's import them at the beginning of our code:

import os
import shutil

Step 2: Define the directory and file path Specify the directory where the files to be merged are located, and the path of the output file. You can modify these paths according to your requirements:

directory = 'path/to/files/'
output_file = 'path/to/output/merged_file.txt'

Step 3: Get the list of files to be merged Use the os.listdir() function to retrieve the list of files in the specified directory. We'll filter out any subdirectories and only consider regular files for merging:

file_list = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]

Step 4: Open the output file in write mode Create a new file with the specified output file path and open it in write mode. We'll use the open() function to do this:

with open(output_file, 'w') as outfile:
# Merge the files into the output file

Step 5: Iterate through the list of files and merge them Inside the with block, iterate through the list of files and read their contents. Append each file's content to the output file:

    for file_name in file_list:
file_path = os.path.join(directory, file_name)
with open(file_path, 'r') as infile:
content = infile.read()
outfile.write(content)

Step 6: Optional - Add separators between merged files If you want to add separators between the content of each merged file, you can use the outfile.write() function to write a separator string after each file is merged:

            outfile.write('\n---\n')  # Add a separator after each file

Step 7: Close the output file After merging all the files, don't forget to close the output file to ensure all the changes are saved:

    outfile.close()

Step 8: Complete code example Here's the complete code example that merges multiple files into a single file:

import os
import shutil

directory = 'path/to/files/'
output_file = 'path/to/output/merged_file.txt'

file_list = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]

with open(output_file, 'w') as outfile:
for file_name in file_list:
file_path = os.path.join(directory, file_name)
with open(file_path, 'r') as infile:
content = infile.read()
outfile.write(content)
outfile.write('\n---\n') # Optional - add a separator after each file
outfile.close()

That's it! You've successfully merged multiple files into a single file using Python. Just make sure to replace the path/to/files/ and path/to/output/ with the actual paths of your files and desired output file.