Skip to content
Advertisement

Using Python Pandas, read multiple folder paths written in xlsx file and process each csv file separately

I have an excel file with the name F_Path.xlsx listing the folder paths like below: enter image description here

path = "C:/Users/Axel/Documents/Work/F_Path.xlsx" 
df_input = pd.read_excel(path1, sheet_name=0) #reading the excel file
folder_path = list(df_input['Folder Path']
path_csv = #1st csv file from C:/Users/Axel/Documents/Work/Folder_1, then 2nd read in for loop, but don't know how.. once all the csv are read from Folder_1, it has to read folder_path[1 to n] and read all the csv files and process it separately.

.
.
.
.
.
df = pd.read_csv(path_csv)  # read all the *.csv file one by one and process each df separately.

#process the data

Advertisement

Answer

Try the following:

# you'll need to import os
import os

# loop your folders
for folder in folder_path:
    # get the csvs in that folder
    csv_files = os.listdir(folder)
    # loop the csvs
    for csvfile in csv_files:
        df = pd.read_csv(os.path.join(folder, csvfile))
        # do your processing here
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement