1

我正在尝试使此函数(搜索给定字符串的目录)也搜索所有子目录,并递归执行此操作。我对 Python 的了解还不够,无法开始。任何指导都会很棒。

谢谢!

def grep(regex, base_dir):
    matches = list()
    for filename in os.listdir(base_dir):
        full_filename = os.path.join(base_dir, filename)
        if not os.path.isfile(full_filename):
            continue
        with open(os.path.join(base_dir, filename)) as fh:
            content = fh.read()
            matches = matches + re.findall(regex, content)
    return matches
4

4 回答 4

2

如果您要爬取整个目录,请尝试os.walk(). 像这样的东西可能有效(未经测试,但如果它不起作用可以调整):

def grep(regex, base_dir):
    matches = list()
    # os.walk() returns a tuple - the directory path, a list of directories and the files
    for dirpath, dirname, files in os.walk(base_dir):
        # Iterate through the directory list, reading the files
        for directory in dirname:
          for filename in os.listdir(directory):
              with open(os.path.join(base_dir, directory, filename)) as fh:
                  content = fh.read()
                  matches = matches + re.findall(regex, content)
    return matches
于 2012-10-25T19:32:12.630 回答
1

我会使用这样的东西:

def find_file_matches(filename, regex):
    with open(filename, 'rt') as fh:
        return re.findall(regex, fh.read())

def walktree(top):
    """ Walk the directory tree starting from top, and
        yield a tuple of each folder and all the files in it. """
    names = os.listdir(top)
    yield top, (name for name in names if not os.path.isdir(name))
    for name in names:
        if os.path.isdir(name):
            for (newtop, children) in walktree(os.path.join(top, name)):
                yield newtop, children

def grep(regex, base_dir="."):
    matches = []
    for dir, files in walktree(base_dir):
        for file in files:
            filename = os.path.join(dir, file)
            matches.append(find_file_matches(filename, regex))
    return matches
于 2012-10-25T21:18:54.477 回答
1

对于递归遍历 try os.walk。你可以在这里找到如何使用它:www.saltycrane.com/blog/2007/03/python-oswalk-example/

于 2012-10-25T19:32:04.487 回答
-1

从命令行

find . -type d | grep -i nameofdir

或类似的东西。

于 2012-10-25T19:29:47.797 回答