Python – Merging multiple CSV files without headers being repeated (using Python)


I am a beginner with Python. I have multiple CSV files (more than 10), and all of them have same number of columns. I would like to merge all of them into a single CSV file, where I will not have headers repeated.

So essentially I need to have just the first row with all the headers and from then I need all the rows from all CSV files merged. How do I do this?

Here's what I tried so far.

import glob
import csv

with open('output.csv','wb') as fout:
    wout = csv.writer(fout,delimiter=',') 
    interesting_files = glob.glob("*.csv") 
    for filename in interesting_files: 
        print 'Processing',filename 
    # Open and process file
        h = True
        with open(filename,'rb') as fin:
        for line in csv.reader(fin,delimiter=','):

Best Solution

If you are on a linux system:

head -1 director/one_file.csv > output csv   ## writing the header to the final file
tail -n +2  director/*.csv >> output.csv  ## writing the content of all csv starting with second line into final file
Related Question