[issue8402] glob returns empty list with "[" character in the folder name

flacs report at bugs.python.org
Fri Nov 11 15:33:11 CET 2011


flacs <0f1ac5 at gmail.com> added the comment:

As a workaround, it is possible to make every glob character a character set of one character (wrapping it with [] ). The gotcha here is that you can't just use multiple replaces because you would escape the escape brackets.

Here is a function adapted from [1]:

def escape_glob(path):
    transdict = {
            '[': '[[]',
            ']': '[]]',
            '*': '[*]',
            '?': '[?]',
            }
    rc = re.compile('|'.join(map(re.escape, transdict)))
    return rc.sub(lambda m: transdict[m.group(0)], path)

[1] http://www.daniweb.com/software-development/python/code/216636

----------
components: +Library (Lib)
nosy: +flacs

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8402>
_______________________________________


More information about the Python-bugs-list mailing list