Python - Read all CSV files in a folder in Pandas?



To read all excel files in a folder, use the Glob module and the read_csv() method. Let’s say the following are our excel files in a directory −

At first, let us set the path and get the csv files. Our CSV files are in the folder MyProject −

path = "C:\Users\amit_\Desktop\MyProject\"

Read files with extension .csv from the above path −

filenames = glob.glob(path + "\*.csv") 

Let us now write a for loop to iterate all csv files, read and print them −

for file in filenames: # reading csv files print("\nReading file = ",file) print(pd.read_csv(file))

Example

Following is the complete code −

 import pandas as pd import glob # getting csv files from the folder MyProject path = "C:\Users\amit_\Desktop\MyProject\" # read all the files with extension .csv filenames = glob.glob(path + "\*.csv") print('File names:', filenames) # for loop to iterate all csv files for file in filenames: # reading csv files print("\nReading file = ",file) print(pd.read_csv(file))

Output

This will produce the following output

File names:['C:\Users\amit_\Desktop\MyProject\Sales1.xlsx','C:\Users\amit_\Desktop\MyProject\Sales2.xlsx'] Reading file = C:\Users\amit_\Desktop\MyProject\Sales1.xlsx           Car      Place   UnitsSold 0        Audi  Bangalore          80 1     Porsche     Mumbai         110 2  RollsRoyce       Pune         100 Reading file = C:\Users\amit_\Desktop\MyProject\Sales2.xlsx           Car       Place UnitsSold 0         BMW       Delhi 95 1    Mercedes   Hyderabad 80 2  Lamborgini  Chandigarh 80
Updated on: 2021-09-27T12:15:29+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements