This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: fnmatch to support escape characters
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add a function to escape metacharacters in glob/fnmatch
View: 8402
Assigned To: Nosy List: eric.araujo, fruch, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2012-02-03 06:50 by fruch, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg152495 - (view) Author: Israel Fruchter (fruch) Date: 2012-02-03 06:50
fnmatch to support escape characters:
like that:

>>> name = "Document[Ver.2].doc"
>>> pattern = "*\[Ver.2\]*"
>>> fnmatch.fnmatch(name, pattern)
True

that's also fix glob module:
>>> pattern = "ipconfig /\?"
>>> glob.glob(pattern)
"ipconfig /?"
msg152590 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-02-04 04:41
The doc chapters are entitled "fnmatch — Unix filename pattern matching" and "glob — Unix style pathname pattern expansion". The first explicitly disclaims the feature you request: "Be aware there is no way to quote meta-characters.", suggests using re for anything beyond fnmatch, and shows to use .translate to make a start in doing so. For your example:

>>> re.match(r".*\[Ver\.2\].*",  "Document[Ver.2].doc")
<_sre.SRE_Match object at 0x000000000331AF38>

Indeed, fnmatch works by using translate() and re.match. What you are asking for in something in between the unix language and re. If one re feature is added, why not another?

So the scope of these modules is clearly circumscribed. I suspect their intent was to make it easy to translate unix shell scripts into Python. 
What you are asking for in something in between. If you want to pursue this, post on python-list or python-ideas to garner more support. But I anticipate rejection as not needed and contrary to intent.

Not obvious from the doc is that an unmatch '[' or ']' is escaped:
>>> name = "Document[Ver.2.doc"
>>> pattern = "*[Ver.2*"
>>> fnmatch.fnmatch(name, pattern)
True
>>> name = "DocumentVer.2].doc"
>>> pattern = "*Ver.2]*"
>>> fnmatch.fnmatch(name, pattern)
True
I presume this matches the *nix behavior, but don't know.
msg152598 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-04 08:11
> [fnmatch] explicitly disclaims the feature you request: "Be aware there is no way to quote
> meta-characters."

This reads like a warning to me, i.e. a potential future feature, not a design choice.

> What you are asking for in something in between the unix language and re. If one re
> feature is added, why not another?

When we use glob patterns in our shells, the shell language lets us escape what would otherwise be special characters.  Python would be nicer to let us do the same.
msg152605 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-02-04 09:48
If indeed fnmatch does not match current shells, then I would agree that it should. It looks to me so easy to add that I though it must be a deliberate decision to exclude. In translate:
...
        elif c == '\':
            if i < n-1:
                c2 = pat[i+1]
                <specify what to do for \c2 for all cases of c2>
            else:
                <specify what to do for pattern ending in '\'>
        else:  
            res = res + re.escape(c)
# the last two lines are current code, which is why '\' in patterns does not escape anything in the translated re.

Changing the meaning of '\' from ordinary character to escape char will break any code that depends on its current ordinariness.
>>> fn.fnmatch(r'\x', r'\?')
True # for x any 'ordinary' char, but not is '\?' means "match '?'.

This was another reason I closed, although I forgot to mention it. I suppose a new parameter 'escape = False' could be added to all 4 exposed functions to preserve back compatibility. Anyway, I have reopened for further discussion and specification.
msg172962 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-15 12:02
Issue8402 has discussion and patch(es).
History
Date User Action Args
2022-04-11 14:57:26adminsetgithub: 58137
2012-10-15 12:02:42serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg172962

superseder: Add a function to escape metacharacters in glob/fnmatch
resolution: duplicate
2012-02-04 09:48:53terry.reedysetstatus: closed -> open
resolution: rejected -> (no value)
messages: + msg152605
2012-02-04 08:11:27eric.araujosetnosy: + eric.araujo
messages: + msg152598
2012-02-04 04:41:11terry.reedysetstatus: open -> closed
versions: - Python 2.6, Python 3.1, Python 2.7, Python 3.2, Python 3.4
nosy: + terry.reedy

messages: + msg152590

resolution: rejected
2012-02-03 06:50:23fruchcreate