Python 如何检查我的文件名列表中是否没有模式列表?

Python 如何检查我的文件名列表中是否没有模式列表?,python,regex,Python,Regex,要放弃一组具有特定文件格式的文件。例如,模式列表中列出的模式 patterns =[r'\.pdf$',r'\.mpeg$'] NOT_FILE = [ re.compile(p) for p in patterns] grep_not_match = (lambda l, regex: [each for each in l if (regex.search(e)))]) for rt,dr,files in os.walk(rootdirecto

要放弃一组具有特定文件格式的文件。例如,模式列表中列出的模式

     patterns =[r'\.pdf$',r'\.mpeg$'] 
     NOT_FILE = [ re.compile(p) for p in patterns] 
     grep_not_match = (lambda l, regex: [each for each in l if (regex.search(e)))])

     for rt,dr,files in os.walk(rootdirectory):
         files = grep_not_match(files,NOT_FILE)

我知道,我在这里犯了一个超级愚蠢的错误,有人能理解吗?

列表理解应该是这样的:[each for each in l if(regex.search(each))]))


似乎这至少是问题的一部分

列表理解应该是这样的:[each for each in l if(regex.search(each))]))


似乎这至少是问题的一部分

像这样的简单任务不需要正则表达式。您只需使用endswith:

files = [f for f in files if not f.endswith(('.pdf', '.mpeg'))]

像这样的简单任务不需要正则表达式。您只需使用endswith:

files = [f for f in files if not f.endswith(('.pdf', '.mpeg'))]
为什么不试试这个模块呢

fnmatch.fnmatch(file, '*.pdf'):
测试文件名字符串是否与模式字符串匹配,返回 真假

为什么不试试这个模块呢

fnmatch.fnmatch(file, '*.pdf'):
测试文件名字符串是否与模式字符串匹配,返回 真假


该死我为什么不这么想呢(谢谢!!该死..我为什么不这么想呢。)(谢谢!!