I am trying to generate a hierarchical directory listing in pyGTK.
Currently, I have this following directory tree:
/root
folderA
- subdirA
- subA.py
- a.py
folderB
- b.py
I have written a function that -almost- seem to work:
def go(root, piter = None):
for filename in os.listdir(root):
isdir = os.path.isdir(os.path.join(root, filename))
piter = self.treestore.append(piter, [filename])
if isdir == True:
go(os.path.join(root, filename), piter)
This is what i get when i run the app:
I also think my function is inefficient and that i should be using os.walk(), since it already exists for such purpose.
How can I, and what is the proper/most efficient way of generating a directory tree with pyGTK?
---edit---- the block of code i ended up using, that works, is:
parents = {}
for dir, dirs, files in os.walk(root):
for subdir in dirs:
parents[os.path.join(dir, subdir)] = self.treestore.append(parents.get(dir, None), [subdir])
for item in files:
self.treestore.append(parents.get(dir, None), [item])