python filecmp.dircmp忽略通配符

时间:2017-09-18 14:09:52

标签: python file compare

filecmp.dircmp用于比较两个文件夹。 它接受参数null,但它只完全匹配而不是通配符匹配。知道如何忽略模式吗?

1 个答案:

答案 0 :(得分:0)

您可以使用全局https://docs.python.org/3/library/glob.html来扩展模式,并将扩展后的列表提供给ignore参数。

让我们说您有左文件夹和右文件夹,并且想要从比较中过滤掉所有* .foo和* .bar,

import os
import glob
import filecmp

ignore_list = []
patterns_to_ignore = ['*.foo', '*.bar']
for pattern in patterns_to_ignore:
    ignore_left = [os.path.split(expanded)[1] for expanded in glob.glob(os.path.join(left, pattern))]
    ignore_right = [os.path.split(expanded)[1] for expanded in glob.glob(os.path.join(right, pattern))]
    ignore_list.extend(ignore_left)
    ignore_list.extend(ignore_right)


diff = filecmp.dircmp(left, right, ignore=ignore_list)