Your attempt almost works, but the problems are:
- You open the file for reading, but close it before writing lines.
- you never write a name you have to write it once
- You should also exclude output.csv from "glob", otherwise the output is also in the input!
Here's the fixed code that passes the csv object directly to the csv.writerows method for shorter and faster code. Also write the header from the first file to the output file.
import glob import csv output_file = 'output.csv' header_written = False with open(output_file,'w',newline="") as fout: # just "wb" in python 2 wout = csv.writer(fout,delimiter=',') # filter out output interesting_files = [x for x in glob.glob("*.csv") if x != output_file] for filename in interesting_files: print('Processing {}'.format(filename)) with open(filename) as fin: cr = csv.reader(fin,delmiter=",") header = cr.next() #skip header if not header_written: wout.writerow(header) header_written = True wout.writerows(cr)
Please note that solutions using raw line-by-line processing miss an important point: if the header is multi-line, it fails miserably, spoiling the title bar several times / repeating part of it, effectively damaging the file.
The CSV module (or pandas too) gracefully handles these cases.
Jean-François Fabre
source share