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
Use shutil.copyfileobj
.
It automatically reads the input files chunk by chunk for you, which is more more efficient and reading the input files in and will work even if some of the input files are too large to fit into memory:
import shutil
with open('output_file.txt','wb') as wfd:
for f in ['seg1.txt','seg2.txt','seg3.txt']:
with open(f,'rb') as fd:
shutil.copyfileobj(fd, wfd)