[英]Is fnmatch.fnmatch incompatible to the Unix pattern filematching syntax?
I am using Python 3.7.6 and the fnmatch.fnmatch function to match a filename with a given pattern.我正在使用 Python 3.7.6 和fnmatch.fnmatch函数将文件名与给定的模式匹配。 Most of my tests worked, but in specific the following example doesn't return the value as expected.
我的大多数测试都有效,但具体而言,以下示例没有按预期返回值。 Give is the following exanoke:
给出以下exanoke:
> fnmatch.fnmatch('_foo\bar.exe', '^_*')
False
What I try to do, to match any filename whose component starts with _
.我尝试做的是匹配其组件以
_
开头的任何文件名。 Any help is highly appreciated任何帮助都受到高度赞赏
This is not a place where Python fails to comply with the POSIX specification.这不是 Python 不符合 POSIX 规范的地方。 UNIX pattern matching syntax does not require
^
to have any special meaning, except in a square-bracket character-set description. UNIX 模式匹配语法不要求
^
具有任何特殊含义,方括号字符集描述除外。
See:看:
fnmatch
function fnmatch
函数的 POSIX 规范This makes sense: Globs are implicitly anchored -- they always match only at the beginning, so there's no reason to support an explicit anchor.这是有道理的:Glob 是隐式锚定的——它们总是只在开始时匹配,因此没有理由支持显式锚定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.