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

2024-06-01 01:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

这必须很简单;如何使用fnmatchos.listdir生成的打印列表中排除一个文件?你知道吗

这个python脚本只列出索引.php文件;我要排除索引.php并列出所有其他.php文件。如果我删除if fnmatch.fnmatch(entry, 'index.php'):,我会得到所有文件的列表,包括索引.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)

Tags: 文件import脚本列表forindexifos
1条回答
网友
1楼 · 发布于 2024-06-01 01:22:47
import os, fnmatch

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

相关问题 更多 >