Gautham N K
Gautham N K

Reputation: 13

Merge multiple csv files in python

Need help with merging multiple csv file

import pandas as pd
import glob
import csv
r1=glob.glob("path/*.csv")
wr1 = csv.writer(open("path/merge.csv",'wb'),delimiter = ',')
for files in r1:
    rd=csv.reader(open(files,'r'), delimiter=',')
    for row in rd:
    print(row)
    wr1.writerow(row)

I am getting a type error TypeError: a bytes-like object is required, not 'str' not sure how to resolve this

Upvotes: 1

Views: 3887

Answers (1)

zipa
zipa

Reputation: 27869

Using pandas you can do it like this:

dfs = glob.glob('path/*.csv')

result = pd.concat([pd.read_csv(df) for df in dfs], ignore_index=True)

result.to_csv('path/merge.csv', ignore_index=True)

Upvotes: 1

Related Questions