Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access files present 2 steps above the root directory in python

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading