gpt4 book ai didi

python - fnmatch 你究竟是如何实现匹配seq模式中的任何字符的

转载 作者:行者123 更新时间:2023-11-28 20:52:06 27 4
gpt4 key购买 nike

所以我有一个os.walk代码

search = self.search.get_text()
top = '/home/bludiescript/tv-shows'
for dirpath, dirnames, filenames in os.walk(top):
for filename in filenames:
if fnmatch.fnmatch(filename, search)
print os.path.join([dirpath, filename])

在 python 文档上,它显示您可以将任何字符序列与 [seq] 模式匹配,但无论我如何尝试实现它都会给出这种错误或根本没有结果。

那么在搜索中匹配汽车序列的正确实现是什么,以便打印出匹配的一个或多个文件

我尝试过的实现

if fnmatch.fnmatch(filename, [search]) error i got was typeerror unhasable type : 'list'
if fnmatch.fnmatch[filename, search] error i got was typeerror fnmatch function is not subscriptable
if fnmatch.fnmatch([filename, search]) error typeerror fnmatch takes two arguments 1 given
if fnmatch.fnmatch([filename], search) error typeerror expected string or buffer
if fnmatch.fnmatch([search], filename) error typeerror expected string or buffer
if fnmatch.fnmatch(filename, search, [seq]) error nameerror global name seq not defined

if fnmatch.fnmatch(filename, '[search]')

没有错误但没有产生任何结果

搜索值

hello, mkv, merry 1, 2, 3, 4, 5, 6, 7, etc...

最佳答案

fnmatch 实现了 Unix shell 通配符语法。因此,无论您在 ls 命令中输入什么(例如)都可以工作:

>>> fnmatch.fnmatch("qxx", "[a-z]xx")
True
>>> fnmatch.fnmatch("abc", "a??")
True
>>> fnmatch.fnmatch("abcdef", "a*f")
True
>>> fnmatch.fnmatch("abcdef", "a*[f-k]")
True

请记住,fnmatch 纯粹是一个字符串匹配操作。如果您发现使用不同的模式样式(例如正则表达式)更方便,则只需使用正则表达式操作来匹配您的文件名。

关于python - fnmatch 你究竟是如何实现匹配seq模式中的任何字符的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7541976/

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