0

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.

5
  • 1
    There's a canonical duplicate for this regardless of what the actual directory structure is, but I think something's wrong with the diagram. Is Test2 supposed to be inside Test, or directly within Auto? Why are there two separate lines pointing at new.csv? Commented Jan 9, 2023 at 22:41
  • @KarlKnechtel I probably did mess up the diagram. I have a folder named “Auto”. Inside the “Auto” folder I have a csv file named “new” and 2 more folders named “Test” and “Test2”. Inside the “Test” folder there is a csv file named “Test” and inside the “Test2” folder there is a csv file named “Test2”. I want to print all 3 csv files. Commented Jan 9, 2023 at 22:47
  • I fixed the diagram to match that description. Please see the linked duplicate for an answer to your question, and meta.stackoverflow.com/questions/420280 for help with making such diagrams. Commented Jan 9, 2023 at 22:50
  • @KarlKnechtel Thank You! I will read through this. Commented Jan 9, 2023 at 22:52
  • Using chdir would really not be a reasonable approach IMO. As noted in the duplicate or the answer below, you don't have to do anything because the glob module can handle this, but even if it couldn't, you should instead use an absolute path and construct it before passing it to glob, don't actually change the working directory Commented Jan 9, 2023 at 23:12

1 Answer 1

1

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) 
Sign up to request clarification or add additional context in comments.

2 Comments

I have changed the code to add recursive=True and it still only shows “new.csv”
Have you also modified your path? See my example, you need **\\*.csv

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.