Search code examples
pythonfileglob

How to combine several text files into one file?


I want to combine several text files into one output files. my original code is to download 100 text files then each time I filter the text from several words and the write it to the output file.

Here is part of my code that suppose to combine the new text with the output text. The result each time overwrite the output file, delete the previous content and add the new text.

import fileinput
import glob    
urls = ['f1.txt', 'f2.txt','f3.txt']

N =0;
print "read files"
for url in urls:
 read_files = glob.glob(urls[N])
 with open("result.txt", "wb") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            outfile.write(infile.read())
 N+=1

and I tried this also

import fileinput
import glob
urls = ['f1.txt', 'f2.txt','f3.txt']

N =0;
print "read files"
for url in urls:
 file_list = glob.glob(urls[N])
 with open('result-1.txt', 'w') as file:
    input_lines = fileinput.input(file_list)
    file.writelines(input_lines)
 N+=1

Is there any suggestions? I need to concatenate/combine approximately 100 text files into one .txt file In sequence manner. (Each time I read one file and add it to the result.txt)


Solution

  • The problem is that you are re-opening the output file on each loop iteration which will cause it to overwrite -- unless you explicitly open it in append mode.

    The glob logic is also unnecessary when you already know the filename.

    Try this instead:

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