Python 搜索目录和子目录

Python 搜索目录和子目录,python,Python,我试图让这个函数(在一个目录中搜索给定字符串)也搜索所有子目录,并递归地执行此操作。我对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(

我试图让这个函数(在一个目录中搜索给定字符串)也搜索所有子目录,并递归地执行此操作。我对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
从命令行

find . -type d | grep -i nameofdir

或者类似的东西。

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

如果您希望对整个目录进行爬网,请尝试
os.walk()
。类似的方法可能会起作用(未经测试,但如果不起作用,可以进行调整):


我会用这样的方式:

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

我想你正在寻找os.walk(path)可能类似于导入os for root,dirs,os.walk(path)中的文件:for name in files:print os.path.join(root,name)?我喜欢这样…看起来我得到了一个:Traceback(最近一次调用):moduleString=grep('require\(\\s*\[^]*\]\\s*\\),“)文件中第46行的文件“dito.py”“ditto.py”,第9行,在grep中打开(os.path.join(dirpath,directory,filename))作为fh:File“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py“,第68行,在join AttributeError:'list'对象没有属性'endswith'@user1738992请立即尝试-显然我没有能力在不进行测试的情况下编写:)该错误已解决,但现在我遇到目录时出现错误:
IOError:[Errno 21]是一个目录:'./tests/testFolder'
谢谢您的帮助@用户1738992没有问题!实际上现在正在开会,但我一回来就会跳下去:)那不是Python。为什么要假设他在linux机器上呢?有时,命令行上的一些简单操作会使程序变得多余。这将在带有MSysGit或Ming的Windows计算机上运行。它还应该在Mac CLI上工作。
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