Mizanur Choudhury
Mizanur Choudhury

Reputation: 346

How to combine glob and pd.read_csv

I'm trying to get a CSV into a dataframe using using pd.read_csv however the csv that I'm loading changes its filename daily (hence why i'm using glob)

This is what i have so far but its spiting out a error Invalid file path or buffer object type: <class 'list'>

import pandas as pd
import glob
csv_load = pd.read_csv(glob.glob(r'C:\Users\mc\pythonprojects\csvconversion\*.csv'))

any ideas?

Upvotes: 0

Views: 468

Answers (1)

elouassif
elouassif

Reputation: 318

pd.concat([pd.read_csv(file) for file in glob.glob(r'C:\Users\mc\pythonprojects\csvconversion\*.csv')])

or if you are sure that there is one file :

csv_load = pd.read_csv(glob.glob(r'C:\Users\mc\pythonprojects\csvconversion\*.csv')[0])

Explanation: golb.glob return a list, so you should choose element from this list

Upvotes: 1

Related Questions