简体   繁体   English

是否可以 os.walk 2 个不同的文件目录并在遍历时将它们相互比较

[英]Is it possible to os.walk 2 different file directories and compare them to each other as it walks through

I know it is possible to iterate over 2 lists in parallel like so...我知道可以像这样并行迭代 2 个列表......

for x, y in zip(xList, yList):
    print x, y

Is it possible to do the same with the os.walk function for 2 file directories?是否可以对 os.walk 函数对 2 个文件目录执行相同操作?

Why not start with for x, y in zip(*map(os.walk, (dir1, dir2,))): ... ?为什么不以for x, y in zip(*map(os.walk, (dir1, dir2,))): ...开头?

Edit: forgot a closing parenthesis编辑:忘记了一个右括号

Yes you can, but the key to making it work is to perform in-place replacement to the sub-directories returned by both generators so that only sub-directories that are common to both of the current directories are retained for deeper traversal and further comparisons;是的,您可以,但使其工作的关键是对两个生成器返回的子目录执行就地替换,以便仅保留两个当前目录共有的子目录以进行更深入的遍历和进一步比较; otherwise the sub-directories produced by the two generators won't align with each other with zip once there is a sub-directory that's unique to one of them:否则,一旦存在对其中一个生成器唯一的子目录,则两个生成器生成的子目录将不会与zip相互对齐:

import os
for (root1, dirs1, files1), (root2, dirs2, files2) in zip(os.walk('dir1'), os.walk('dir2')):
    set1, set2 = set(dirs1), set(dirs2)
    print('Directories unique to {}: {}'.format(root1, set1 - set2))
    print('Directories unique to {}: {}'.format(root2, set2 - set1))
    dirs1[:] = dirs2[:] = set1 & set2 # crucial to retain only the common directories
    set1, set2 = set(files1), set(files2)
    print('Files unique to {}: {}'.format(root1, set1 - set2))
    print('Files unique to {}: {}'.format(root1, set2 - set1))

See demo: https://repl.it/repls/WholePessimisticSource见演示: https : //repl.it/repls/WholePessimisticSource

From the documentation of os.walk :os.walk的文档中:

When topdown is True , the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames ;topdownTrue ,调用者可以dirnames修改dirnames列表(可能使用del或 slice 赋值),而walk()只会递归到名称保留在dirnames的子目录; this can be used to prune the search...这可用于修剪搜索...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM