Open In App

How to read all CSV files in a folder in Pandas?

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Our task is 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

  1. Import Required Libraries: We’ll need pandas for data manipulation, glob to find CSV files, and os for file path handling.
  2. Use glob() to Find CSV Files: The glob module helps us retrieve all CSV files matching the pattern *.csv in a specified directory.
  3. Loop Through the Files: After finding the files, we’ll loop through each file, read it into a DataFrame, and append it to a list of DataFrames.
  4. Display the Content: For each CSV file, we’ll print its file location, name, and content.
FolderForCSVfiles

Folder containing CSV files

Implementation:

Python
import pandas as pd
import os
import glob

# use glob to get all the csv files in the folder
path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))


# loop over the list of csv files
for f in csv_files:
    
    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

Explanation: The code imports pandas, os, and glob to work with CSV files. It uses glob to get a list of all .csv files in the current directory, then reads each file into a Pandas DataFrame using pd.read_csv(). The DataFrames are appended to a list, and the code prints the file’s location, name, and a preview of its content. After reading all the files, pd.concat() is used to combine the DataFrames into a single one, which is displayed at the end.

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


Next Article
Practice Tags :

Similar Reads