以下代码列出了目录中以"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/