pythoncsvglobchdir

Using chdir() to read all csv files in a directory


Here is an example of my directory:

Auto
├── Test
│   └── Test.csv
├── Test2
│   └── Test2.csv
└── new.csv

I am trying to read all the csv files in the directory.

Here is my code:

import glob, os

os.chdir('C:\\Users\\Me\\Desktop\\Auto')
for file in glob.iglob('*.csv'):
    print(file)

This only prints “new.csv” and not “Test.csv” or “Test2.csv”.

I was thinking maybe I could try the directory name as the following:

os.chdir('C:\\Users\\Me\\Desktop\\Auto\\{}')

But, this gives me a FileNotFoundFoundError as the system cannot find the directory specified.


Solution

  • glob iterates by default only through the root dir, no subdirectories

    import glob, os
    
    for file in glob.iglob(r'C:\Users\Me\Desktop\Auto\**\*.csv', recursive=True):
        print(file)