user16031555
user16031555

Reputation:

How to concatenate a large number of text files in python

I have 367 text files I want to concatenate into one long text file. I was thinking I could go about this by doing:

filenames = ["file1.txt","file2.txt","file3.txt"...]
with open("output_file.txt","w") as outfile:
    for filename in filenames:
        with open(filename) as infile:
            contents = infile.read()
            outfile.write(contents)

However, this would require me to write out every single text file name, which would be very tedious. Is there an easier way to concatenate a large number of text files using Python? Thank you!

Upvotes: 0

Views: 2395

Answers (2)

Buddy Bob
Buddy Bob

Reputation: 5889

Assuming these files all have a counter (1-367) in the format filename-count-.extension, we can generate 367 files using list comprehension.

filenames = [f'file{i}.txt' for i in range(1,368)]
with open("output_file.txt","w") as outfile:
    for filename in filenames:
        with open(filename) as infile:
            contents = infile.read()
            outfile.write(contents)

another option you could use is os.listdir.

import os
for i in os.listdir('files'):
    print(i)

output

text1.txt
text2.txt

Upvotes: 3

Corralien
Corralien

Reputation: 120439

Use glob.glob to build list files and shutil.copyfileobj for a more efficient copy.

import shutil
import glob

filenames = glob.glob("*.txt")  # or "file*.txt"
with open("output_file.txt", "wb") as outfile:
    for filename in filenames:
        with open(filename, "rb") as infile:
            shutil.copyfileobj(infile, outfile)

Upvotes: 3

Related Questions