The*_*One 1 python os.walk os.path
我想找到一种使用Python以自下而上的方式遍历目录树的方法.目标是找到一个目录,该目录在当前目录之上或之下是未知距离.
我做了一个图表,我希望我的问题更清楚:

红色框是起始点,绿色框是目标文件夹的可能位置,其中只需要找到一个,而不是两者.
如果目标框是/ One/_One/_One,那么我希望脚本上升到/ One,然后一直遍历到/ One/_One/_One.
如果目标文件夹是/ Three,那么我希望脚本执行与上面相同的操作,然后继续执行/ Two和/ Two/_Two,找不到它,然后最终到达/ Three.或者,在检查所有/ One后,它会转到/然后找到/ Three,跳过/ Two的遍历.
任何帮助,将不胜感激.我一直在查看所有的os.path和os.walk方法,但还没有找到我的解决方案.
关键在于以下文档os.walk:
当topdown为True时,调用者可以就地修改dirnames列表(可能使用del或slice赋值),而walk()只会递归到名称保留在dirnames中的子目录中
有了这个,您只需将此问题视为树搜索,您可以从第一个节点的根开始,每次找不到解决方案时,都会弹出一个级别并再次执行树搜索,当你到达那里时,删除作为你上次搜索的根的节点.
假设我有以下内容:
start_path = 'ABC0123/Comp/Scripts'
searching_for ='Some_File'
Run Code Online (Sandbox Code Playgroud)
我可以做以下事情:
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)
Run Code Online (Sandbox Code Playgroud)
第一次迭代应该搜索'ABC0123/Comp/Scripts'目录.然后,如果找不到'Some_File',它将搜索'ABC0123/Comp'目录,跳过'Scripts'目录.然后它将搜索'ABC0123'目录,跳过'Comp'它下面的所有内容.
这是遍历的一些示例输出.CR是current_root,LR是last_root,并且Exploring是root在的当前步骤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'
Run Code Online (Sandbox Code Playgroud)
另请注意,您是否正在搜索目录或文件并不是很清楚.我的代码假定后者,但如果是前者只是改变:
if searching_for in files:
Run Code Online (Sandbox Code Playgroud)
至
if searching_for in dirs:
Run Code Online (Sandbox Code Playgroud)
但请注意,在这两种情况下,假设您正在搜索的是唯一的,全局的(在最大树深度内)唯一文件/目录,或者您遇到的该文件/目录的第一个实例是您的那个寻找.例如,如您所写,您无法专门搜索"Paint/Output".您应该能够非常轻松地弄清楚如何修改搜索条件以允许这样做.
| 归档时间: |
|
| 查看次数: |
2938 次 |
| 最近记录: |