[英]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
isTrue
, the caller can modify thedirnames
list in-place (perhaps usingdel
or slice assignment), andwalk()
will only recurse into the subdirectories whose names remain indirnames
;当
topdown
为True
,调用者可以dirnames
修改dirnames
列表(可能使用del
或 slice 赋值),而walk()
只会递归到名称保留在dirnames
的子目录; this can be used to prune the search...这可用于修剪搜索...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.