简体   繁体   English

Python:“反转” fnmatch以匹配一个文件名?

[英]Python: “invert” fnmatch to match one file name?

This has to be simple; 这必须很简单; how do I exclude one file using fnmatch from a printed list generated by os.listdir ? 如何使用fnmatchos.listdir生成的打印列表中排除一个文件?

This python script only lists the index.php file; 这个python脚本仅列出index.php文件; I want to exclude index.php and list all other .php files. 我想排除index.php并列出所有其他.php文件。 If I remove if fnmatch.fnmatch(entry, 'index.php'): I get a list of all files including index.php. 如果删除if fnmatch.fnmatch(entry, 'index.php'):我会得到所有文件的列表,包括index.php。 Is it possible to "invert" fnmatch ? 是否可以“反转” 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)
import os, fnmatch

listOfFiles = os.listdir('.')  
pattern = "*.php"  
for entry in listOfFiles:   
    if entry != "index.php" and fnmatch.fnmatch(entry, pattern):
            print(entry)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM