Skip to content

Instantly share code, notes, and snippets.

@taco-shellcode
Created November 21, 2019 17:48
Show Gist options
  • Save taco-shellcode/bdaa3c75a23b898994956bb89b271bcb to your computer and use it in GitHub Desktop.
Save taco-shellcode/bdaa3c75a23b898994956bb89b271bcb to your computer and use it in GitHub Desktop.
import os
import csv
directory = "/path/to/csv_folder/"
headers = []
merged_file_data = []
for file_name in os.listdir(directory):
index = 0
file_path = os.path.join(directory, file_name)
with open(file_path, "r") as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
index += 1
if index == 1 and not headers:
headers.append(line)
elif index == 1:
continue
else:
merged_file_data.append(line)
with open("output.csv", "w") as outfile:
csv_writer = csv.writer(outfile)
csv_writer.writerows(headers + merged_file_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment