How to read all CSV files in a folder in Pandas?
In this article, we will see how to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV files in a particular folder using glob() method and then reading the file by using pandas.read_csv() method and then displaying the content.
Approach:
- Import necessary python packages like pandas, glob, and os.
- Use glob python package to retrieve files/pathnames matching a specified pattern i.e. ‘.csv’
- Loop over the list of csv files, read that file using pandas.read_csv().
- Convert each csv file into a dataframe.
- Display its location, name, and content.
Below is the implementation.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python3
# import necessary librariesimport pandas as pdimport osimport glob # use glob to get all the csv files # in the folderpath = os.getcwd()csv_files = glob.glob(os.path.join(path, "*.csv")) # loop over the list of csv filesfor f in csv_files: # read the csv file df = pd.read_csv(f) # print the location and filename print('Location:', f) print('File Name:', f.split("\\")[-1]) # print the content print('Content:') display(df) print() |
Output:



Note: The program reads all CSV files in the folder in which the program itself is present.

