【问题标题】:Python search for filename pattern within a specific directory patternPython 在特定目录模式中搜索文件名模式
【发布时间】:2013-12-03 14:35:05
【问题描述】:

如何使用 os.walk(或任何其他方式)进行搜索,以便我可以在根目录下具有特定模式的目录下找到具有特定名称的文件

我的意思是,如果我有一个目录 d:\installedApps,在该目录下我有 a.ear、b.ear、... x.ear、y.ear、z.ear 目录以及其他目录级别,我想只在根目录下的*.ear子目录下搜索文件模式web*.xml,而不遍历同级别的其他目录,我该怎么做?

我尝试了各种方法(一些使用本网站上的其他示例,例如 walklevel 示例等),但没有得到我想要的结果。

更新

我尝试使用来自该站点的 walkdepth 代码 sn-p 并尝试将其组合在一个嵌套循环中,但这不起作用

这是我试过的代码

import os, os.path
import fnmatch

def walk_depth(root, max_depth):
    print 'root in walk_depth : ' + root
    # some initial setup for getting the depth
    root = os.path.normpath(root)
    depth_offset = root.count(os.sep) - 1

    for root, dirs, files in os.walk(root, topdown=True):
        yield root, dirs, files
        # get current depth to determine if we need to stop
        depth = root.count(os.sep) - depth_offset
        if depth >= max_depth:
            # modify dirs so we don't go any deeper
            dirs[:] = []

for root, dirs, files in walk_depth('D:\installedApps', 5):
    for dirname in dirs:
        if fnmatch.fnmatch(dirname, '*.ear'):
            print 'dirname : ' + dirname
            root2 = os.path.normpath(dirname)
            for root2, dir2, files2 in walk_depth(root2, 5):
                for filename in files2:
                    if fnmatch.fnmatch(filename, 'webservices.xml'):
                        print '\tfilename : ' + filename

【问题讨论】:

    标签: python pattern-matching filenames directory-traversal


    【解决方案1】:

    我强烈建议您查看此答案。有三种不同的解决方案,但 #1 似乎最准确地符合您想要做的事情。

    Find all files in a directory with extension .txt in Python

    编辑我刚刚找到了一些关于可能完成这项工作的 glob 类的更多信息。

    来自 Python 文档

    glob.glob(路径名)

    返回与路径名匹配的可能为空的路径名列表,其中 必须是包含路径规范的字符串。路径名可以是 绝对(如/usr/src/Python-1.5/Makefile)或相对(如 ../../Tools/*/*.gif),并且可以包含 shell 样式的通配符。破碎的 符号链接包含在结果中(就像在 shell 中一样)。

    所以你可以按照以下方式做一些事情:

    def getFiles(path):
        answer = []
        endPaths = glob.glob(path + "web*.xml")
        answer += endPaths
        if len(glob.glob(path + "*ear/")) > 0:
                answer += getFiles(path + "*ear/")
    
        return answer
    
    filepaths = getFiles("./")
    print(filepaths
    

    )

    我实际上测试了那个,它在我认为按照你想要的方式设置的目录中工作得非常好。

    【讨论】:

    • 谢谢。我确实看过它来做文件名的模式。我想我在嵌套它时遇到了麻烦,我不必遍历根级别的所有目录,而只查看 *.ear 目录,然后只在这些目录中查找特定文件。
    • 哦,对不起,我猜你的问题是这样的。我觉得这必须手动完成,我对 Python 中的文件遍历没有太多经验。稍后我可能会用新的解决方案回复您,但我现在工作有点忙。
    • for root, dirs, files in walk_depth('D:\installedApps', 5): for dirname in dirs: if fnmatch.fnmatch(dirname, '*.ear'): print 'dirname : ' + dirname root2 = os.path.normpath(dirname) for root2, dir2, files2 in walk_depth(root2, 5): for fname in files2: if fnmatch.fnmatch(fname, 'webservices.xml'): print '\tfilename : ' + 文件名
    • 只是想告诉你我找到了一个符合我对你的问题的理解的答案。
    • 哦,对不起,我真的以为你说的只是一个深度。我今天很难集中注意力:p。无论如何,您有一个递归解决方案。你可以让它更高效/模块化或者你有什么我只是想让代码清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多