有没有办法从使用python的目录比较中排除特定的文件类型

时间:2016-04-25 16:48:10

标签: python directory ignore

这就是我所拥有的:

import filecmp
from filecmp import dircmp
def print_right_only(dcmp):
     for name in dcmp.right_only:
         print "%s not found in %s but found in %s" % (name, dcmp.left,
            dcmp.right)
     for sub_dcmp in dcmp.subdirs.values():
         print_right_only(sub_dcmp)

dcmp = dircmp('E:\GIS', 'J:\GIS') 
print_right_only(dcmp)

这完全符合要求。我希望能够从结果中过滤或排除* .lock文件。我确实看到了一个与dircmp相关的忽略函数但无法让它返回任何结果。我是python的一个非常基本的用户。

2 个答案:

答案 0 :(得分:1)

Func<dynamic, dynamic> DynamicProperty(Func<dynamic, dynamic> selector) { return x => { try { return selector(x); } catch (RuntimeBinderException) { return null; } }; } &#39; var sorted = list .OrderBy(DynamicProperty(x => x.property1)) .ThenBy(DynamicProperty(x => x.property2)); 并不接受整个文字。

通过将dircomp替换为:

来解决这个问题并不难
ignore

在致电filecmp._filter之前执行此操作,然后from fnmatch import fnmatch def _filter(flist, skip): return [item for item in flist if not any(fnmatch(item, pat) for pat in skip)] filecmp._filter = _filter 将按预期工作。

未测试。

答案 1 :(得分:0)

事后可以进行过滤吗?

import filecmp
from filecmp import dircmp
def print_right_only(dcmp):
     for name in dcmp.right_only:

         if name.endswith('.lock'):  # Check if it is a lock file.
             continue

         print "%s not found in %s but found in %s" % (name, dcmp.left,
            dcmp.right)
     for sub_dcmp in dcmp.subdirs.values():
         print_right_only(sub_dcmp)

dcmp = dircmp('E:\GIS', 'J:\GIS') 
print_right_only(dcmp)