combine multiple text files into one text file using python [duplicate]

You can read the content of each file directly into the write method of the output file handle like this:

import glob

read_files = glob.glob("*.txt")

with open("result.txt", "wb") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            outfile.write(infile.read())

Leave a Comment