Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting list of sub directories inside a given directory

I am facing difficulty in getting the xml structure listing all the directories/ sub directories inside a given directory. I got that working using the recursion in the given post My problem is little bit tougher than usual. I have directories that may have 10000 of files in it so checking every content to see if its a directory or not is going to be costly and its already taking to long to build the xml. I want to build the xml for directories only.

I know linux has some command like find . -type d to list the directories present (not the files). How can I achieve this in python.

Thanks in advance.

like image 725
Anurag Sharma Avatar asked Jan 16 '23 11:01

Anurag Sharma


1 Answers

os.walk already distinguishes between files and directories:

def find_all_dirs(root='.'):
    for path,dirs,files in os.walk(root):
        for d in dirs:
            yield os.path.join(path, d)
like image 200
phihag Avatar answered Jan 18 '23 23:01

phihag