2014-04-10 16 views
6

ho python 2.7 e sto cercando di emettere:espansione Brace in python glob

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

ottengo nessun risultato, ma dalla shell echo {faint,bright*}/{science,calib}/chip? dà:

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 

Cosa c'è di sbagliato con la mia espressione ?

+0

I don' Penso che il modulo glob supporti le parentesi graffe, vedi http://bugs.python.org/issue9584 –

+0

Il [modulo '' fnmatch'] (https://docs.python.org/2/library/fnmatch.html) (usato da 'glob' per implementare la corrispondenza del nome file) non è sofisticato quanto il supporto' {. ..} 'contrasta la sintassi di espansione. –

risposta

3

Dal {} non fanno parte glob() in Python, che probabilmente si desidera è qualcosa di simile

import os 
import 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) 
3

{..} è noto come espansione della controventatura ed è un passaggio separato applicato prima del globbing.

Non fa parte dei glob e non è supportato dalla funzione python glob.

2

Prova https://pypi.python.org/pypi/braceexpand

pip install braceexpand 

Demo:

>>> from braceexpand import braceexpand 

# Integer range 
>>> list(braceexpand('item{1..3}')) 
['item1', 'item2', 'item3'] 

# Nested patterns 
>>> list(braceexpand('python{2.{5..7},3.{2,3}}')) 
['python2.5', 'python2.6', 'python2.7', 'python3.2', 'python3.3']