python - 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)
推荐阅读
- idris - 函数类型的类型列表
- javascript - 如何使用 JavaScript/CSS 制作圆形 ScrollBox?
- python - 写入 txt 文件时删除所有引号
- android - 使用 RxBus 将对象传输到片段
- mysql - SQL 根据不同行中的值更改 id 的另一行
- python - 如何修复命令“python setup.py egg_info”失败?
- python - 填充 Pandas 数据框中两列之间的数字
- android - TextView 文本不显示
- ios - fancybox3 图像按钮 iframe youtube 视频
- visual-studio-code - 使用户能够减小活动栏上的图标大小