gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:04:16 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com