I'm very new to scripting and as a result am not sure how best to merge a series of files. I'm attempting to create a Quality Control script that makes sure a nightly load was properly uploaded to the DB (we've noticed that if there's a lag for some reason, the sync will exclude any donations that came in during said lag).
I have a directory of daily synced files labeled as such:
for i in a.txt b.txt c.txt d.txt
Expanding on Alex Hall's answer, you can grab the header from one file and skip it for the remaining files to do the merge
from glob import glob from shutil import copyfileobj files = sorted(glob('*_donations.txt'))[-7:] # if you want most recent file first do # files.reverse() with open("merged_file.txt", "w") as outfp: for i, filename in enumerate(files): with open(filename) as infile: if i: next(infile) # discard header copyfileobj(infile, outfile) # write remaining