2011-01-09 92 views
2

我想獲得適用於我有的正則表達式的文件列表。我想我應該使用os.walk,但我怎樣才能用它與正則表達式?os.walk與正則表達式

謝謝。

+5

請張貼一些代碼你試圖使用。請發佈您提出的解決方案,包括您收到的任何錯誤消息。這不是「www.do-my-job-for-me.com」 – 2011-01-09 14:00:38

回答

5

如果您的正則表達式可以翻譯成shell表達式,例如foo/*.txt那麼您可以使用glob

>>> import glob 
>>> glob.glob('./[0-9].*') 
['./1.gif', './2.txt'] 
>>> glob.glob('*.gif') 
['1.gif', 'card.gif'] 
>>> glob.glob('?.gif') 
['1.gif'] 
6

我不知道在什麼STDLIB實現這一點,但它並不難代碼:

import os, os.path 

def iter_matching(dirpath, regexp): 
    """Generator yielding all files under `dirpath` whose absolute path 
     matches the regular expression `regexp`. 
     Usage: 

      >>> for filename in iter_matching('/', r'/home.*\.bak'): 
      .... # do something 
    """ 
    for dir_, dirnames, filenames in os.walk(dirpath): 
     for filename in filenames: 
      abspath = os.path.join(dir_, filename) 
      if regexp.match(abspath): 
       yield abspath 

或者更一般的:

import os, os.path 

def filter_filenames(dirpath, predicate): 
    """Usage: 

      >>> for filename in filter_filenames('/', re.compile(r'/home.*\.bak').match): 
      .... # do something 
    """ 
    for dir_, dirnames, filenames in os.walk(dirpath): 
     for filename in filenames: 
      abspath = os.path.join(dir_, filename) 
      if predicate(abspath): 
       yield abspath