代码之家  ›  专栏  ›  技术社区  ›  BlueDogRanch

python:“invert”fnmatch匹配一个文件名?

  •  0
  • BlueDogRanch  · 技术社区  · 7 年前

    这必须很简单;如何使用 fnmatch 从由生成的打印列表 os.listdir ?

    这个python脚本只列出index.php文件;我想排除index.php并列出所有其他.php文件。如果我移除 if fnmatch.fnmatch(entry, 'index.php'): 我得到所有文件的列表,包括index.php。是否可以“反转” Fnmatch公司 ?

    import os, fnmatch
    
    listOfFiles = os.listdir('.')  
    pattern = "*.php"  
    for entry in listOfFiles:  
    
     if fnmatch.fnmatch(entry, 'index.php'):
    
        if fnmatch.fnmatch(entry, pattern):
                print (entry)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   xrisk    7 年前
    import os, fnmatch
    
    listOfFiles = os.listdir('.')  
    pattern = "*.php"  
    for entry in listOfFiles:   
        if entry != "index.php" and fnmatch.fnmatch(entry, pattern):
                print(entry)