astabada astabada - 8 months ago 47
Bash Question

Python glob does not expand expression correctly

I have python 2.7 and am trying to issue:


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

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?


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

import os
import re


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