Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fnmatch() vs. glob.glob()

1,453 views
Skip to first unread message

Jinghui Niu

unread,
Mar 7, 2016, 3:17:39 PM3/7/16
to
Hi, I've been studying python 3 modules. I'm a bit confused about the possibly overlap between fnmatch() and glob(), they seem to achieve the same goals exactly. Why duplicate?
Or maybe something I've missed?

Thank you.

Ben Finney

unread,
Mar 7, 2016, 4:37:45 PM3/7/16
to
Jinghui Niu <niuji...@gmail.com> writes:

> Hi, I've been studying python 3 modules. I'm a bit confused about the
> possibly overlap between fnmatch() and glob(), they seem to achieve
> the same goals exactly. Why duplicate?

>From the module documentation:

Note that unlike fnmatch.fnmatch(), glob treats filenames beginning
with a dot (.) as special cases.

<URL:https://docs.python.org/2/library/glob.html>

So the goals are different: ‘glob.glob’ has the goal of matching closer
to the Unix meaning of glob patterns, where filenames starting with a
“.” character are conventionally treated as “hidden by default”.

--
\ “We are stuck with technology when what we really want is just |
`\ stuff that works.” —Douglas Adams |
_o__) |
Ben Finney

Jinghui Niu

unread,
Mar 7, 2016, 7:53:18 PM3/7/16
to
> `\ stuff that works." --Douglas Adams |
> _o__) |
> Ben Finney

Thank you for your reply. So if for a beginner learner who doesn't care so much about Unix-compliant, which one is a better choice for learning purpose?

Ben Finney

unread,
Mar 7, 2016, 8:00:57 PM3/7/16
to
Jinghui Niu <niuji...@gmail.com> writes:

> Thank you for your reply. So if for a beginner learner who doesn't
> care so much about Unix-compliant, which one is a better choice for
> learning purpose?

Learn to care about the difference :-) They are both useful and both do
different things.

--
\ “I must say that I find television very educational. The minute |
`\ somebody turns it on, I go to the library and read a book.” |
_o__) —Groucho Marx |
Ben Finney

Jinghui Niu

unread,
Mar 7, 2016, 8:04:32 PM3/7/16
to
> _o__) --Groucho Marx |
> Ben Finney

May I take this opportunity to ask a bold question for a beginner: if I want to mimic Sublime Text's fuzzy search for a file search in given directories, which module of these two would be more suitable? Could you shed some insights? Thanks.

Ben Finney

unread,
Mar 7, 2016, 8:16:44 PM3/7/16
to
Jinghui Niu <niuji...@gmail.com> writes:

> May I take this opportunity to ask a bold question for a beginner

No problem, your questions are on topic here. Thank you for being civil.

> if I want to mimic Sublime Text's fuzzy search for a file search in
> given directories, which module of these two would be more suitable?

You'll need to describe the requirements, I don't know exactly what that
behaviour is.

--
\ “He that loveth father or mother more than me is not worthy of |
`\ me: and he that loveth son or daughter more than me is not |
_o__) worthy of me.” —Jesus, as quoted in Matthew 10:37 |
Ben Finney

Jinghui Niu

unread,
Mar 7, 2016, 8:37:24 PM3/7/16
to
> _o__) worthy of me." --Jesus, as quoted in Matthew 10:37 |
> Ben Finney

Let me try to describe the behaviours as much as I can here: It is a real-time search, updating the search result as you type; It is very smart, not limiting itself into the verbatim words, but extracting feature strings automatically, e.g., typing "fontz" will match not just "*fontz*", but also "font-size", "fontzipper", etc; It can be switched for certain type of files according specific rules, such as not including ".pyc" files.

My gut feeling is that it is using Regex, but just wonder how I can fit Regex into either fnmatch() or glob() module.

Ben Finney

unread,
Mar 7, 2016, 8:50:19 PM3/7/16
to
Jinghui Niu <niuji...@gmail.com> writes:

> Let me try to describe the behaviours as much as I can here: It is a
> real-time search, updating the search result as you type; It is very
> smart, not limiting itself into the verbatim words, but extracting
> feature strings automatically, e.g., typing "fontz" will match not
> just "*fontz*", but also "font-size", "fontzipper", etc; It can be
> switched for certain type of files according specific rules, such as
> not including ".pyc" files.

None of that really depends on the difference between ‘glob’ versus
‘fnmatch’. Either of them could be at the bowels of an implementation of
the behaviour you describe.

> My gut feeling is that it is using Regex, but just wonder how I can
> fit Regex into either fnmatch() or glob() module.

You don't. Those two are *much* lower level than the behaviour you
describe, and don't have anything to say about that behaviour.

You will need all the extra framework implementing all the other stuff
you describe, before you make any decision between ‘fnmatch’ versus
‘glob’.

--
\ “Skepticism is the highest duty and blind faith the one |
`\ unpardonable sin.” —Thomas Henry Huxley, _Essays on |
_o__) Controversial Questions_, 1889 |
Ben Finney

Chris Angelico

unread,
Mar 7, 2016, 8:56:47 PM3/7/16
to
On Tue, Mar 8, 2016 at 12:37 PM, Jinghui Niu <niuji...@gmail.com> wrote:
> Let me try to describe the behaviours as much as I can here: It is a real-time search, updating the search result as you type; It is very smart, not limiting itself into the verbatim words, but extracting feature strings automatically, e.g., typing "fontz" will match not just "*fontz*", but also "font-size", "fontzipper", etc; It can be switched for certain type of files according specific rules, such as not including ".pyc" files.
>
> My gut feeling is that it is using Regex, but just wonder how I can fit Regex into either fnmatch() or glob() module.

Sounds to me like you're looking at two different things.

1) File name matching, whether with fnmatch or glob, will tell you
which files to look at. You mention excluding .pyc files; you could
specify that only "*.py" be matched, or "*q*.py", or whatever.

2) Regular expression or other searching, within the files.

Either fnmatch or glob will serve you well for the first; I don't
think they're appropriate for the second. But it depends what you're
trying to do.

ChrisA

Steven D'Aprano

unread,
Mar 7, 2016, 9:19:53 PM3/7/16
to
On Tue, 8 Mar 2016 12:04 pm, Jinghui Niu wrote:

> May I take this opportunity to ask a bold question for a beginner: if I
> want to mimic Sublime Text's fuzzy search for a file search in given
> directories, which module of these two would be more suitable? Could you
> shed some insights? Thanks.

Read the documentation. See what functionality each module provides.

Here is the documentation for Python 3:

https://docs.python.org/3/library/glob.html
https://docs.python.org/3/library/fnmatch.html


and for Python 2:

https://docs.python.org/2/library/glob.html
https://docs.python.org/2/library/fnmatch.html


Reading the source is also a great way to learn about the module, and to
learn what is considered best practice in Python:


https://hg.python.org/cpython/file/3.5/Lib/glob.py
https://hg.python.org/cpython/file/3.5/Lib/fnmatch.py



This may also be helpful:


https://pymotw.com/2/glob/
https://pymotw.com/2/fnmatch/



But I shall give you a hint: the glob module imports the fnmatch module, and
uses it to perform the matching.


--
Steven

MRAB

unread,
Mar 7, 2016, 9:35:40 PM3/7/16
to
Could it be as simple as looking for _those_ characters in _that_ order?

It might be giving a higher prominence to those whose matching letters
are closer together.

0 new messages