def filename_match(filename, patterns):
"""
Check if patterns contains a pattern that matches filename.
"""
# `dir/*` works but `dir/` does not
for index in range(len(patterns)):
if patterns[index][-1] == '/':
patterns[index] += '*'
# filename has a leading `/` which confuses fnmatch
filename = filename.lstrip('/')
# Pattern is a fnmatch compatible regex
if any(fnmatch.fnmatch(filename, pattern) for pattern in patterns):
return True
# Pattern is a simple name of file or directory (not caught by fnmatch)
for pattern in patterns:
if '/' not in pattern and pattern in filename.split('/'):
return True
return False
评论列表
文章目录