Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read files from multiple folders in python

my folder organization looks like below. Type 1 and Type 2 folders contains same files but I want to only read the files from 'type 2' folder. Is there any simple way to do that?

I have used this code but not able to read:

for file in os.listdir('Type 2'):
    print file

folder organization

your help will be highly appreciated!

like image 673
bikuser Avatar asked Jul 22 '26 18:07

bikuser


1 Answers

IIUC you need read_csv in folders Type 2, use glob:

files = glob.glob('main/**/Type 2/*.csv')
dfs = [pd.read_csv(fp) for fp in files]
df = pd.concat(dfs)
like image 56
jezrael Avatar answered Jul 25 '26 08:07

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!