使用Python自底向上遍历目录树

时间:2014-07-23 19:46:49

标签: python os.walk os.path

我想找到一种使用Python以自​​下而上的方式遍历目录树的方法。目标是找到一个目录,该目录在当前目录之上或之下是未知距离。

我做了一个图表,希望我的问题更清楚: Directory Tree

红色框是起始点,绿色框是目标文件夹的可能位置,其中只需要找到一个,而不是两者。

如果目标框是/ One / _One / _One,那么我希望脚本上升到/ One,然后一直遍历到/ One / _One / _One。

如果目标文件夹是/ Three,那么我希望脚本执行与上面相同的操作,然后继续执行/ Two和/ Two / _Two,找不到它,然后最终到达/ Three。或者,在检查所有/ One之后,它会转到/然后找到/ Three,跳过/ Two的遍历。

任何帮助将不胜感激。我一直在查看所有的os.path和os.walk方法,但还没有找到我的解决方案。

1 个答案:

答案 0 :(得分:6)

关键在于os.walk的以下文档:

  

当topdown为True时,调用者可以就地修改dirnames列表(可能使用del或slice赋值),而walk()只会递归到名称保留在dirnames中的子目录

有了这个,您只需将此问题视为树搜索,您可以从第一个节点的根开始,每次找不到解决方案时,都会弹出一个级别并执行再次进行树搜索,删除当你到达那里时作为上次搜索根目录的节点

假设我有以下内容:

start_path    = 'ABC0123/Comp/Scripts'
searching_for ='Some_File'

我可以做以下事情:

last_root    = start_path
current_root = start_path
found_path   = None
while found_path is None and current_root:
    pruned = False
    for root, dirs, files in os.walk(current_root):
        if not pruned:
           try:
              # Remove the part of the tree we already searched
              del dirs[dirs.index(os.path.basename(last_root))]
              pruned = True
           except ValueError:
              pass
        if searching_for in files:
           # found the file, stop
           found_path = os.path.join(root, searching_for)
           break
     # Otherwise, pop up a level, search again
     last_root    = current_root
     current_root = os.path.dirname(last_root)

第一次迭代应搜索'ABC0123/Comp/Scripts'目录。然后,如果找不到'Some_File',它将搜索'ABC0123/Comp'目录,跳过'Scripts'目录。然后它将搜索'ABC0123'目录,跳过'Comp'及其下的所有内容。

这是遍历的一些示例输出。 CRcurrent_rootLRlast_root,而Exploringroot当前步骤中的walk。在这种情况下,文件位于ABC0123/Paint/Output

CR: 'ABC0123/Comp/Scripts/', LR: 'ABC0123/Comp/Scripts/'
Exploring: 'ABC0123/Comp/Scripts/'
CR: 'ABC0123/Comp/Scripts', LR: 'ABC0123/Comp/Scripts/'
Exploring: 'ABC0123/Comp/Scripts'
CR: 'ABC0123/Comp', LR: 'ABC0123/Comp/Scripts'
Exploring: 'ABC0123/Comp'
Exploring: 'ABC0123/Comp/Output'
CR: 'ABC0123', LR: 'ABC0123/Comp'
Exploring: 'ABC0123'
Exploring: 'ABC0123/Lighting'
Exploring: 'ABC0123/Lighting/Output'
Exploring: 'ABC0123/Paint'
Exploring: 'ABC0123/Paint/Output'

>>> found_path
'ABC0123/Paint/Output/Some_File'

另请注意,您是否正在搜索目录或文件并不是很清楚。我的代码假设后者,但如果是前者只是改变:

if searching_for in files:

if searching_for in dirs:

但请注意,在这两种情况下,假设您正在搜索的是唯一的,全局的(在最大树深度内)唯一文件/目录,或者您遇到的该文件/目录的第一个实例是您正在寻找。例如,如您所写,您无法专门搜索“Paint / Output”。您应该能够很容易地弄清楚如何修改搜索条件以允许这样做。