Python 目录遍历器 pyqt

Python directory walker pyqt

我正在尝试在 python 中编写一个目录遍历器来确定查找文件类型。我的问题是我可能在我的子文件夹中引用了父文件夹,从而导致循环。有什么办法可以克服这个问题吗?我的代码如下所示:

def _hasMp3(path, fileTypes):
   if path:
     childDirs = [root]
     fileTypes = fileTypes or []
     while childDirs:
        qdir = childDirs.pop()
        path = unicode(qdir.absolutePath())
        path = os.path.normalpath(path)
    return False 

获取每个目录的解析路径,并保留一组以前见过的:

 seen = set()
 while subDirs:
    qdir = subDirs.pop()
    path = unicode(qdir.canonicalPath())
    if path in seen:
        continue
    seen.add(path)