How to get all grandchildren of a directory in python with only one OS call
python
Solution
if i got your question right.
you can use glob to get files or directories, by giving wildcard notations. for example to get all dir inside "/home/" in a list you can do.
glob.glob('/home/*/*/')
or to know all the files as well you can do
glob.glob('/home/*/*')
Problem
I am trying to get all grandchildren of a certain directory in Python. For performance reasons I don't want to keep calling OS functions in a loop (it's a network filesystem). This is what I have at the moment. Is there a simpler way to do this? ``` dirTree = os.walk(root) children = [os.path.join(root, x) for x in dirTree.next()[1]] grandChildren = [] for root, dirs, files in dirTree: if root in children: for dir in dirs: grandChildren.append(os.path.join(root, dir)) ``` EDIT: I'm not clear on whether my call to os.walk is lazy or not. My intention is that the whole tree should be in memory after my call but I'm not sure about it.