Question

I dont want to use commands or any similar module that uses unix shell. Thanks in advance..

Was it helpful?

Solution

If you want to do this recursively, going down a tree visiting all the directories, then you can use os.walk like this:

  for root, directories, files in os.walk("c:\\"):
    doSomething

If you only want the subdirectories you can either call walk once:

  directories = os.walk("c:\\").next()[1]

Or do some sort of filter like this (walk is more stylish/portable):

  filter(lambda x: os.path.isdir("c:\\"+ x), os.listdir("c:\\"))

OTHER TIPS

Use os.walk():

import os, os.path

def walk_directories(src):
    for root, dirs, files in os.walk(src):
        for dir in dirs:
            print os.path.join(root, dir)

walk_directories(r'c:\temp')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top