I have a list of 20 file names, like [\'file1.txt\', \'file2.txt\', ...]
. I want to write a Python script to concatenate these files into a new file. I could op
If you have a lot of files in the directory then glob2
might be a better option to generate a list of filenames rather than writing them by hand.
import glob2
filenames = glob2.glob('*.txt') # list of all .txt files in the directory
with open('outfile.txt', 'w') as f:
for file in filenames:
with open(file) as infile:
f.write(infile.read()+'\n')