1

What would be the best method of getting sub directories of a drive including files located within them? Would it be best to use os.listdir() and filter out directories from files by checking if they have a '.' in them?

Any ideas would be helpful, and i would much prefer that i use only the standard library for this task.

2
  • You have given an idea, why have you not tried it? If you have, why didn't it work? Commented May 30, 2012 at 16:54
  • os.path.isdir(full_path) will tell you if something is a directory. Commented May 30, 2012 at 17:15

1 Answer 1

4

Take a look at os.walk(), it allows you to visit each directory and get a list of files and a list of sub directories for each directory that you visit.

Here is how you could only go down a single level:

for root, dirs, files in os.walk(path):
    # do whatever you want to with dirs and files
    if root != path:
        # one level down, modify dirs in place so we don't go any deeper
        del dirs[:]
Sign up to request clarification or add additional context in comments.

1 Comment

The OP seems to want to go only one level down.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.