glob & fnmatch unable to match dirs/files containing [ ] ?
John Machin
sjmachin at lexicon.net
Sat Mar 22 18:12:49 EST 2003
"Robert" <k.robert at gmx.de> wrote in message news:<3e7caaac$0$21717$9b622d9e at news.freenet.de>...
> glob/fnmatch uses [seq] matching.
> On Windows and Linux brackets are allowed in filenames:
The fnmatch module doco says "Unix". Linux isn't Unix. However all is
not lost; read on.
> 04.06.2002 16:24 <DIR> CVS
> 08.11.2002 21:49 <DIR> testsync[2] (contains files)
>
> >>> glob.glob('testsync[2]/*')
> []
>
> obviously would match only on testsync2.
> adding 'testsync2':
>
> 04.06.2002 16:24 <DIR> CVS
> 08.11.2002 21:49 <DIR> testsync[2] (contains files)
> 21.03.2003 15:43 <DIR> testsync2 (contains other
> files)
>
> >>> glob.glob('testsync[2]/*')
> ['testsync2\\x.txt', 'testsync2\\y.txt', 'testsync2\\z.txt',
> 'testsync2\\testsync.syncpy.syncdb']
>
> delivers the content of the other dir testsync2 !
As expected.
>
> >>> fnmatch.fnmatch('testsync[2]','testsync[2]')
> 0
>
> >>> fnmatch.fnmatch('testsync2','testsync[2]')
> 1
>
> quoting attempts don't work:
>
> >>> glob.glob( r'testsync\[2\]/*' ) # or 'testsync\\[2\\]/*'
> []
That isn't quoting. That's escaping.
>
> This would work in the linux shell like: ro at lx:~/acer > ll testsync\[2\]/*
> #works
> Maybe this quote should be supported also in future glob/fnmatch versions?
Ugh. No. Backslashorrhea. Besides, will it be backward-compatible?
>
> Does anybody know a simple remedy.
Try this:
>>> import fnmatch
>>> fnmatch.fnmatch("foo[2]bar", "foo[[]2]bar")
1
>>>
More information about the Python-list
mailing list