还获取与fnmatch不匹配的元素 [英] Get also elements that don't match fnmatch
本文介绍了还获取与fnmatch不匹配的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用递归glob来查找文件并将文件从驱动器复制到另一个驱动器
I'm using a recursive glob to find and copy files from a drive to another
def recursive_glob(treeroot, pattern):
results = []
for base, dirs, files in os.walk(treeroot):
goodfiles = fnmatch.filter(files, pattern)
results.extend(os.path.join(base, f) for f in goodfiles)
return results
工作正常.但是我也想访问那些与过滤器不匹配的元素.
Works fine. But I also want to have access to the elements that don't match the filter.
有人可以提供一些帮助吗?我可以在循环中构建一个正则表达式,但是必须有一个更简单的解决方案,对吧?
Can someone offer some help? I could build a regex within the loop, but there must be a simpler solution, right?
推荐答案
如果顺序无关紧要,请使用一组:
If order doesn't matter, use a set:
goodfiles = fnmatch.filter(files, pattern)
badfiles = set(files).difference(goodfiles)
这篇关于还获取与fnmatch不匹配的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文