[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

"glob.glob('weirdness')" Any thoughts?

On 10/09/18 14:40, Max Zettlmei?l via Python-list wrote:
> On Sun, Sep 9, 2018 at 6:03 PM, Thomas Jollans <tjol at tjol.eu> wrote:
>> https://docs.python.org/3/library/glob.html#glob.escape demonstrates a way
>> of escaping that works:
>> glob('./Testfile [[]comment]*')
> That is about the least correct working solution one could conceive.
> Of course your suggested "glob('./Testfile [[]comment]*')" works in
> the positive case, but pretty much comes down to a glob('./Testfile
> [[]*').
> And in the negative case it would provide many false positives. (e.g.
> "Testfile [falacy]", "Testfile monty", "Testfile ]not quite" and so
> on)
> Even if you wanted to use that strange character class, which is not a
> good idea (as explained above), using "[[]coment]" would be better,
> since there is no reason to repeat a character.

 >>> from glob import glob
 >>> glob('test *')
['test comment', 'test [co]mment', 'test [fallacy]', 'test [comments]', 
'test [comment] a']
 >>> glob('test [[]*')
['test [co]mment', 'test [fallacy]', 'test [comments]', 'test [comment] a']
 >>> glob('test [[]c*')
['test [co]mment', 'test [comments]', 'test [comment] a']
 >>> glob('test [[]comment]*')
['test [comment] a']

I'm escaping the '[' as '[[]'. You can escape the ']' as well if you 
want, but there's no need as a ']' is not special unless it's preceded 
by an unescaped '['.

To match the character class I think you thought my glob was matching, 
you'd have to use '[][comment]' rather than '[[]comment]'.

-- Thomas