How can I merge 200 CSV files in Python?

As ghostdog74 said, but this time with headers:

with open("out.csv", "ab") as fout:
    # first file:
    with open("sh1.csv", "rb") as f:
        fout.writelines(f)
    # now the rest:    
    for num in range(2, 201):
        with open("sh"+str(num)+".csv", "rb") as f:
            next(f) # skip the header, portably
            fout.writelines(f)

Leave a Comment