Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104252 > unrolled thread

fnmatch() vs. glob.glob()

Started byJinghui Niu <niujinghui@gmail.com>
First post2016-03-07 12:17 -0800
Last post2016-03-08 13:19 +1100
Articles 11 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  fnmatch() vs. glob.glob() Jinghui Niu <niujinghui@gmail.com> - 2016-03-07 12:17 -0800
    Re: fnmatch() vs. glob.glob() Ben Finney <ben+python@benfinney.id.au> - 2016-03-08 08:37 +1100
      Re: fnmatch() vs. glob.glob() Jinghui Niu <niujinghui@gmail.com> - 2016-03-07 16:53 -0800
        Re: fnmatch() vs. glob.glob() Ben Finney <ben+python@benfinney.id.au> - 2016-03-08 12:00 +1100
          Re: fnmatch() vs. glob.glob() Jinghui Niu <niujinghui@gmail.com> - 2016-03-07 17:04 -0800
            Re: fnmatch() vs. glob.glob() Ben Finney <ben+python@benfinney.id.au> - 2016-03-08 12:16 +1100
              Re: fnmatch() vs. glob.glob() Jinghui Niu <niujinghui@gmail.com> - 2016-03-07 17:37 -0800
                Re: fnmatch() vs. glob.glob() Ben Finney <ben+python@benfinney.id.au> - 2016-03-08 12:49 +1100
                Re: fnmatch() vs. glob.glob() Chris Angelico <rosuav@gmail.com> - 2016-03-08 12:56 +1100
                Re: fnmatch() vs. glob.glob() MRAB <python@mrabarnett.plus.com> - 2016-03-08 02:35 +0000
            Re: fnmatch() vs. glob.glob() Steven D'Aprano <steve@pearwood.info> - 2016-03-08 13:19 +1100

#104252 — fnmatch() vs. glob.glob()

FromJinghui Niu <niujinghui@gmail.com>
Date2016-03-07 12:17 -0800
Subjectfnmatch() vs. glob.glob()
Message-ID<d8ec5d47-3393-41c3-a06c-0ec058763570@googlegroups.com>
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.

[toc] | [next] | [standalone]


#104256

FromBen Finney <ben+python@benfinney.id.au>
Date2016-03-08 08:37 +1100
Message-ID<mailman.50.1457386649.10335.python-list@python.org>
In reply to#104252
Jinghui Niu <niujinghui@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

[toc] | [prev] | [next] | [standalone]


#104288

FromJinghui Niu <niujinghui@gmail.com>
Date2016-03-07 16:53 -0800
Message-ID<14061576-1c44-49bb-a27b-279e43258f06@googlegroups.com>
In reply to#104256
On Monday, March 7, 2016 at 1:37:45 PM UTC-8, Ben Finney wrote:
> Jinghui Niu <niujinghui@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

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?

[toc] | [prev] | [next] | [standalone]


#104290

FromBen Finney <ben+python@benfinney.id.au>
Date2016-03-08 12:00 +1100
Message-ID<mailman.15.1457398847.15725.python-list@python.org>
In reply to#104288
Jinghui Niu <niujinghui@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

[toc] | [prev] | [next] | [standalone]


#104291

FromJinghui Niu <niujinghui@gmail.com>
Date2016-03-07 17:04 -0800
Message-ID<7475bb64-b2e7-45ff-a94e-dbc62f658fab@googlegroups.com>
In reply to#104290
On Monday, March 7, 2016 at 5:00:57 PM UTC-8, Ben Finney wrote:
> Jinghui Niu <niujinghui@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

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.

[toc] | [prev] | [next] | [standalone]


#104293

FromBen Finney <ben+python@benfinney.id.au>
Date2016-03-08 12:16 +1100
Message-ID<mailman.17.1457399791.15725.python-list@python.org>
In reply to#104291
Jinghui Niu <niujinghui@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

[toc] | [prev] | [next] | [standalone]


#104300

FromJinghui Niu <niujinghui@gmail.com>
Date2016-03-07 17:37 -0800
Message-ID<a0d8dba4-834b-4e28-95a6-43f8667fe2ce@googlegroups.com>
In reply to#104293
On Monday, March 7, 2016 at 5:16:44 PM UTC-8, Ben Finney wrote:
> Jinghui Niu <niujinghui@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

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.

[toc] | [prev] | [next] | [standalone]


#104307

FromBen Finney <ben+python@benfinney.id.au>
Date2016-03-08 12:49 +1100
Message-ID<mailman.23.1457401807.15725.python-list@python.org>
In reply to#104300
Jinghui Niu <niujinghui@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

[toc] | [prev] | [next] | [standalone]


#104308

FromChris Angelico <rosuav@gmail.com>
Date2016-03-08 12:56 +1100
Message-ID<mailman.24.1457402196.15725.python-list@python.org>
In reply to#104300
On Tue, Mar 8, 2016 at 12:37 PM, Jinghui Niu <niujinghui@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

[toc] | [prev] | [next] | [standalone]


#104314

FromMRAB <python@mrabarnett.plus.com>
Date2016-03-08 02:35 +0000
Message-ID<mailman.27.1457404526.15725.python-list@python.org>
In reply to#104300
On 2016-03-08 01:37, Jinghui Niu wrote:
> On Monday, March 7, 2016 at 5:16:44 PM UTC-8, Ben Finney wrote:
>> Jinghui Niu <niujinghui@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
>
> 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.
>
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.

[toc] | [prev] | [next] | [standalone]


#104312

FromSteven D'Aprano <steve@pearwood.info>
Date2016-03-08 13:19 +1100
Message-ID<56de36bc$0$1602$c3e8da3$5496439d@news.astraweb.com>
In reply to#104291
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web