git.net

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

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


On Mon, Sep 10, 2018 at 3:05 PM, Thomas Jollans <tjol at tjol.eu> wrote:
>>>> 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]'.
>

That is of course correct.
I'm sorry. Now that I looked at it again, I can't see how I came to
that wrong conclusion.
Your suggested "[][comment]" is exactly what I thought your
"[[]comment]" to be and I can't explain to myself anymore how I came
to that conclusion.

There actually is glob.ecape [1] which escapes all the glob
meta-characters in a path and does so exactly as in your example.
(Since it is obviously the shortest and therefore in this case best
way.)

[1] https://docs.python.org/3/library/glob.html#glob.escape