combine multiple text files into one text file using python -


suppose have many text files follows:

file1:

abc def ghi 

file2:

abc def ghi 

file3:

adfafa 

file4:

ewrtwe rewrt wer wrwe 

how can make 1 text file below:

result:

abc def ghi abc def ghi adfafa ewrtwe rewrt wer wrwe 

related code may be:

import csv import glob files = glob.glob('*.txt') file in files: open('result.txt', 'w') result: result.write(str(file)+'\n') 

after this? help?

you can read content of each file directly write method of output file handle this:

import glob  read_files = glob.glob("*.txt")  open("result.txt", "wb") outfile:     f in read_files:         open(f, "rb") infile:             outfile.write(infile.read()) 

Comments