Skip to content

Instantly share code, notes, and snippets.

@akanik
Last active March 7, 2019 03:31
Show Gist options
  • Save akanik/f5e1a540a07de0d462d046e8796008b1 to your computer and use it in GitHub Desktop.
Save akanik/f5e1a540a07de0d462d046e8796008b1 to your computer and use it in GitHub Desktop.
combine csv files into one
import os
data_dir = '/Users/akanik/data/csv/'
file1 = 'whatever-data-1.csv'
file1_path = data_dir + file1
#Make sure your combined file does not live within your data_dir.
#This will cause an endless loop of writing
combined_file = '/Users/akanik/data/whatever-data-ALL.csv'
fout = open(combined_file,'a')
# first file:
for line in open(file1_path):
fout.write(line)
# now the rest:
for file in os.listdir(data_dir):
if file != file1 and file.endswith('.csv'):
f = open(data_dir + file)
f.next() # skip the header
for line in f:
fout.write(line)
f.close() # not really needed
fout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment