Best Practice to Python Combine Multiple Text Files into One Text File

By | April 27, 2020

Sometimes, we need combine some text files into one file to read and process. We can implement this function easily in python. In this tutorial, we will illustrate python beginners how to do.

Python Combine Multiple Text Files into One Text File

Traverse all text files in a directory

To combine text files, we should get all text files in a directory, you can traverse all text files in a directory using pyhong. Here are tutorials:

Python Traverse Files in a Directory Using glob Library: A Beginner Guide

Python Traverse Files in a Directory for Beginners – Python Tutorial

Save the content of all text files into a big text file

We can open a text file one by one, then save all content into a new file.

Here is an example:

import glob

dir = r'E:\Large Movie Review Dataset\aclImdb\test\neg'

files = glob.glob(dir + '\\*.txt')

In this code, we will get all .txt files in E:\Large Movie Review Dataset\aclImdb\test\neg.

Then we can read all data in these files into a new one.

file_big = 'aclImdb.txt'

with open(file_big, 'wb') as fnew:
    for f in files:
        with open(f, 'rb') as fold:
            for line in fold:
                fnew.write(line)
                fnew.write("\n".encode(encoding='utf_8'))

In this code, we will read all .txt files to a new .txt file aclImdb.txt.

Run this code, you will get the result.

Best Practice to Python Combine Multiple Text Files into One Text File