我在目录中有一堆zip文件,如果其中一个丢失,我们希望收到通知。
示例代码:
a = ['pattern1.zip', 'pattern2.zip', 'pattern3.zip']
b = []
for root,dirs,files in os.walk(some_path):
for i in files:
if i.endswith(('pattern1.zip', 'pattern2.zip', 'pattern3.zip')):
b.append(i)
输出: b = ['test-pattern1.zip','test-pattern2.zip','test-pattern3.zip']
想要将'b'的内容与'a'匹配,并检查是否缺少任何zip文件
答案 0 :(得分:3)
我会采取不同的方法:
patterns = {'pattern1.zip', 'pattern2.zip', 'pattern3.zip'}
for root, dirs, files in os.walk(some_path):
for f in files:
for pattern in patterns:
if f.endswith(pattern):
patterns.remove(pattern)
break
print('Missing patterns:', patterns)
答案 1 :(得分:3)
您可以将列表转换为集合并区分1:
files_that_should_be_present = ['pattern1.zip', 'pattern2.zip', 'pattern3.zip']
files_that_are_present = ['pattern1.zip', 'pattern2.zip']
files_missing = list(set(files_that_should_be_present) - set(files_that_are_present))
print(files_missing)
输出:['pattern3.zip']