glob & fnmatch unable to match dirs/files containing [ ] ?

Peter Hansen peter at engcorp.com
Sun Mar 23 00:50:15 EST 2003


John Machin wrote:
> 
> On Sat, 22 Mar 2003 18:33:45 -0500, Peter Hansen <peter at engcorp.com>
> wrote:
> >I'm not certain, but I believe you would need
> >to change the order to  'testsync[][2]'
> 
> Attaining certainty is not very bothersome:
> >>> fnmatch.fnmatch("foo[2]bar", "foo[][2]bar")
> 0

Hmm... it was quite bothersome when I was running off and 
didn't have time to think of testing, or even checking
the documentation (which I would normally have done).
I'm glad you found it less bothersome and troubled to correct
me.

> > ... this
> >looks like an empty class then one with a single 2
> >in it, but it's not.
> 
> Oh yes it is.
> >>> fnmatch.fnmatch("foo2bar", "foo[][2]bar")
> 1
> 
> > Yours would look like a single
> > [ in a class, then a 2 and then... syntax error or
> >something,
> 
> Please try some examples and/or look at the source instead of just
> guessing.

I was going by my understanding of the "re" module, where I remembered 
reading: """If you want to include a "]" or a "-" inside a set, precede 
it with a backslash, or place it as the first character. The 
pattern []] will match ']', for example."""  I see now that this 
was exactly what was being done, but I thought the intention was
for all three characters, ], [, and 2, to be inside the set.

I suppose you could argue that any incorrect response to a question
is a "guess", but I would disagree with that and find your advice 
to be rudely presented.  (And, yes, I'm aware I'm sometimes 
thin-skinned: I don't need advice about that, too. :-) )

-Peter




More information about the Python-list mailing list