gpt4 book ai didi

Why do both fnmatch and glob exist?(为什么fnMatch和Globb都存在?)

转载 作者:bug小助手 更新时间:2023-10-25 17:43:18 27 4
gpt4 key购买 nike



I already have some clues, but since I couldn't find a clear and concise answer anywhere, I thought it might benefit other people as well.

我已经有了一些线索,但由于我在任何地方都找不到明确而简洁的答案,我想这可能也会让其他人受益。


There are two modules in Python which do file name matching, namely fnmatch and glob.

在Python中有两个模块执行文件名匹配,即fnMatch和Glob。


Among other things, one of the differences between the two is that fnmatch does not treat file names starting with a period as being hidden. However, they still seem to perform relatively similar tasks, which makes me wonder why they're split as two separate modules instead of one with their combined functionality.

两者之间的区别之一是,fnMatch不会将以句点开头的文件名视为隐藏文件名。然而,它们似乎仍然执行相对相似的任务,这让我想知道为什么它们被分成两个独立的模块,而不是一个具有组合功能的模块。


Is there a good reason for this split or is it just so for Python historical reasons?

这种拆分是有充分的原因吗,还是仅仅是因为Python的历史原因?


更多回答

I'm not very familiar, myself, but according to this answer, after glob breaks the path apart into directory and filename, it internally goes ahead and analyzes each portion using fnmatch.

我自己不是很熟悉,但根据这个答案,在GLOB将路径分解为目录和文件名之后,它在内部继续并使用fnMatch分析每个部分。

fnmatch() just matches one file to a pattern and returns a boolean. glob.glob() returns a list of all the files that match.

FnMatch()只将一个文件与一个模式匹配,并返回一个布尔值。GLOB()返回匹配的所有文件的列表。

fnmatch is independent of any filesystem - you could use it with a list of filenames coming from a database, for example. note that glob actually uses fnmatch.

FnMatch独立于任何文件系统--例如,您可以将其与来自数据库的文件名列表一起使用。请注意,GLOB实际上使用的是fnMatch。

优秀答案推荐

fnmatch() just matches one filename against the pattern, returning True or False. If you want a list of matching filenames you need to write your own code that calls os.listdir().

FnMatch()只根据模式匹配一个文件名,返回True或False。如果需要匹配文件名列表,则需要编写自己的代码来调用os.listdir()。


glob.glob() matches the pattern against all the names in the directory. It allows wildcards in the directory portions, and it will do the matching at each directory level. And if you use the recursive=True option, you can use ** in the directory portion to have it automatically walk the directory (like os.walk()) looking for anything that matches the remainder.

GLOB()将模式与目录中的所有名称进行匹配。它允许在目录部分中使用通配符,并且将在每个目录级别进行匹配。如果您使用RECURSIVE=True选项,则可以在目录部分使用**让它自动遍历目录(如os.walk()),以查找与剩余部分匹配的任何内容。


So glob.glob() is a higher level convenience function that hides lots of details.

因此,lob.lobb()是一个隐藏了许多细节的更高级别的便利函数。



glob() explores your filesystem while fnmatch() does not.

Glob()探索您的文件系统,而fnMatch()则不会。


fnmatch is used in the glob implementation.

在GLOB实现中使用了fnMatch。


更多回答

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