bikuser
bikuser

Reputation: 2093

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!

Upvotes: 7

Views: 11602

Answers (1)

jezrael
jezrael

Reputation: 862431

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)

Upvotes: 12

Related Questions