I have an excel file with the name F_Path.xlsx listing the folder paths like below:
JavaScript
x
14
14
1
path = "C:/Users/Axel/Documents/Work/F_Path.xlsx"
2
df_input = pd.read_excel(path1, sheet_name=0) #reading the excel file
3
folder_path = list(df_input['Folder Path']
4
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.
5
6
.
7
.
8
.
9
.
10
.
11
df = pd.read_csv(path_csv) # read all the *.csv file one by one and process each df separately.
12
13
#process the data
14
Advertisement
Answer
Try the following:
JavaScript
1
12
12
1
# you'll need to import os
2
import os
3
4
# loop your folders
5
for folder in folder_path:
6
# get the csvs in that folder
7
csv_files = os.listdir(folder)
8
# loop the csvs
9
for csvfile in csv_files:
10
df = pd.read_csv(os.path.join(folder, csvfile))
11
# do your processing here
12