Brace expansion in python glob Brace expansion in python glob shell shell

Brace expansion in python glob


{..} is known as brace expansion, and is a separate step applied before globbing takes place.

It's not part of globs, and not supported by the python glob function.


Combining globbing with brace expansion.

pip install braceexpand

Sample:

from glob import globfrom braceexpand import braceexpanddef 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']


Since {} aren't supported by glob() in Python, what you probably want is something like

import osimport re...match_dir = re.compile('(faint|bright.*)/(science|calib)(/chip)?')for dirpath, dirnames, filenames in os.walk("/your/top/dir")    if match_dir.search(dirpath):        do_whatever_with_files(dirpath, files)        # OR        do_whatever_with_subdirs(dirpath, dirnames)