I dont want to use commands or any similar module that uses unix shell. Thanks in advance..
+2
A:
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')
hughdbrown
2009-12-17 13:05:41
A:
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:\\"))
Nick Fortescue
2009-12-17 13:19:36