简体   繁体   中英

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 ?

This python script only lists the index.php file; I want to exclude index.php and list all other .php files. If I remove if fnmatch.fnmatch(entry, 'index.php'): I get a list of all files including index.php. Is it possible to "invert" 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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