python - 列出目录中不匹配模式的文件

标签 python glob

以下代码列出了目录中以"hello"开头的所有文件:

import glob
files = glob.glob("hello*.txt")

如何选择不是以 "hello" 开头的其他文件?

最佳答案

如何仅使用 glob:

匹配所有文件:

>>> glob.glob('*')
['fee.py', 'foo.py', 'hello.txt', 'hello1.txt', 'test.txt', 'text.txt']
>>>

只匹配hello.txt:

>>> glob.glob('hello*.txt')
['hello.txt', 'hello1.txt']
>>>

匹配没有字符串 hello:

>>> glob.glob('[!hello]*')
['fee.py', 'foo.py', 'test.txt', 'text.txt']
>>>

匹配没有字符串 hello 但以 .txt 结尾的:

>>> glob.glob('[!hello]*.txt')
['test.txt', 'text.txt']
>>>

关于python - 列出目录中不匹配模式的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22625616/

相关文章:

python - 理解列表的问题(我认为)

python - 如何在 python 中创建 3D 高度图

python - 替换每行中小于 "n"最大项的 numpy 数组的所有值

python - Keras模型训练内存泄漏

python - TkInter:利用错误的位置

bash - 在 case 语句中检查 '?' 的正确方法是什么

c++ - cmake glob包含同时保留目录结构

perl - 从后到上读取日志文件 - perl

xcode - *~ 在 glob 中是什么意思?

linux - 使用 zsh globbing 进行 grepping 时排除目录