简体   繁体   English

os.walk与正则表达式

[英]os.walk with regex

I'd like to get a list of files that apply to a regex that i have. 我想获得一个适用于我所拥有的正则表达式的文件列表。 I guess i should use os.walk, but how can i use it with regex? 我想我应该使用os.walk,但我如何使用正则表达式?

Thanks. 谢谢。

I'm not aware of anything in the stdlib implementing this, but it is not hard to code: 我不知道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

Or the more general: 或者更一般:

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

If your regex can be translated into a shell expression such as foo/*.txt then you can use glob . 如果您的正则表达式可以转换为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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM