How to list directory hierarchy in GtkTreeView widget?

雨燕双飞 提交于 2019-12-18 18:40:10

问题


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])

回答1:


About the performance, this is a FAQ. About your algorithm: when you reach subdirA piter points to subdirA, at the next iteration when you reach a.py piter still points to subdirA.

As you said, use os.walk.



来源:https://stackoverflow.com/questions/2551147/how-to-list-directory-hierarchy-in-gtktreeview-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!