import csv
import os
from pathlib import Path
from os import walk
path = os.path.join(os.path.expanduser('~'), 'Documents')
f = []
for (dirpath, dirnames, filenames) in walk(path):
f.extend(filenames)
break
inputs = next(walk("~//Documents"), (None, None, []))[2]
# inputs = ["in1.csv", "in2.csv"] # etc
print(str(len(f))+" files found in "+ path)
print(Path.cwd())
# First determine the field names from the top line of each input file
# Comment 1 below
fieldnames = []
for filename in f:
with open(os.path.join(path,filename), "r", newline="") as f_in:
reader = csv.reader(f_in)
headers = next(reader)
for h in headers:
if h not in fieldnames:
fieldnames.append(h)
print(fieldnames)
# Then copy the data
with open("out.csv", "w", newline="") as f_out: # Comment 2 below
writer = csv.DictWriter(f_out, fieldnames=fieldnames)
writer.writeheader()
for filename in f:
with open(os.path.join(path,filename), "r", newline="") as f_in:
reader = csv.DictReader(f_in) # Uses the field names in this file
for line in reader:
# Comment 3 below
writer.writerow(line)
To embed this program on your website, copy the following code and paste it into your website's HTML: