Search code examples
pythonbashshellglobexpansion

Brace expansion in python glob


I have python 2.7 and am trying to issue:

glob('{faint,bright*}/{science,calib}/chip?/')

I obtain no matches, however from the shell echo {faint,bright*}/{science,calib}/chip? gives:

faint/science/chip1 faint/science/chip2 faint/calib/chip1 faint/calib/chip2 bright1/science/chip1 bright1/science/chip2 bright1w/science/chip1 bright1w/science/chip2 bright2/science/chip1 bright2/science/chip2 bright2w/science/chip1 bright2w/science/chip2 bright1/calib/chip1 bright1/calib/chip2 bright1w/calib/chip1 bright1w/calib/chip2 bright2/calib/chip1 bright2/calib/chip2 bright2w/calib/chip1 bright2w/calib/chip2

What is wrong with my expression?


Solution

  • Combining globbing with brace expansion.

    pip install braceexpand
    

    Sample:

    from glob import glob
    from braceexpand import braceexpand
    
    def braced_glob(path):
        l = []
        for x in braceexpand(path):
            l.extend(glob(x))
                
        return l
    
    >>> braced_glob('/usr/bin/{x,z}*k')  
    ['/usr/bin/xclock', '/usr/bin/zipcloak']