Home > Software engineering >  How to access files present 2 steps above the root directory in python
How to access files present 2 steps above the root directory in python

Time:12-14

I have a root directory consisting of many folders and that sub folder has another folder and then csv file is present

Root Folder
->Sub1
->F1
->File1.csv
->Sub2
->F2
->File2.csv

I want to give the path of root directory. Go through each subfolder , take the csv as a dataframe and go to another folder, take that csv as dataframe and so on. Can anyone please tell me how to do it?

CodePudding user response:

One option itterrate through all subfolders until csv is found and append them to list

import pandas as pd
import os.path
import os

root_dir = "/dir/"
    
for root, dirs, files in os.walk(root_dir ):
    csv_list= []
    for filename in files:
        if filename.endswith('.csv'):
            csv_list.append(os.path.join(root, filename)) 

your csv_list will looks like this

print(csv_list)

Gives #

['fakepath/f1.csv', fakepath/f2.csv'...]

Itterrate over list and create a df.

import pandas as pd
df= (pd.read_csv(csv) for csv in csv_list)
final_df= pd.concat(df, ignore_index=True)
  • Related