- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to concatenate two files into a new file using Python?
To merge multiple files in a new file, you can simply read files and write them to a new file using loops.
For example
filenames = ['file1.txt', 'file2.txt', 'file3.txt'] with open('output_file', 'w') as outfile: for fname in filenames: with open(fname) as infile: outfile.write(infile.read())
If you have very big files, instead of writing them at once, you could write them line by line.
For example
filenames = ['file1.txt', 'file2.txt', 'file3.txt'] with open('output_file', 'w') as outfile: for fname in filenames: with open(fname) as infile: for line in infile: outfile.write(line)
- Related Articles
- How to merge multiple files into a new file using Python?
- How to spilt a binary file into multiple files using Python?
- Merge contents of two files into a third file using C
- Java program to merge two files into a third file
- How to copy files to a new directory using Python?
- How are files added to a tar file using Python?
- How are files added to a zip file using Python?
- Python Program to Concatenate Two Dictionaries Into One
- How are files extracted from a tar file using Python?
- Java program to merge two or more files alternatively into third file
- How to extract all the .txt files from a zip file using Python?
- How to write into a file from command line using Python?
- How to save files using a File Chooser in JavaFX?
- How to concatenate two strings using Java?
- How to concatenate two strings in Python?

Advertisements