Skip to content

Instantly share code, notes, and snippets.

@danwaterfield
Last active April 3, 2019 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danwaterfield/41e84f236b055f1b1d931ac3dcfc1d5c to your computer and use it in GitHub Desktop.
Save danwaterfield/41e84f236b055f1b1d931ac3dcfc1d5c to your computer and use it in GitHub Desktop.
Merges n text files into a single csv, ignoring the headers in all but the first text file.
import glob
interesting_files = glob.glob("*.txt")
header_saved = False
with open('output.csv','w') as fout:
for filename in interesting_files:
with open(filename) as fin:
header = next(fin)
if not header_saved:
fout.write(header)
header_saved = True
for line in fin:
fout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment