Python仅列出包含特定子文件夹的文件夹

Python仅列出包含特定子文件夹的文件夹,python,python-2.7,Python,Python 2.7,我有一个脚本,将列出所有文件夹和子文件夹,并创建一个JSON文件。我所要做的只是列出包含名为“地图”或“报告”的子文件夹的文件夹。如果它们包含这些内容,则只会列出父文件夹,因此不会显示“地图”、“报告”。目前仍停留在如何实现这一点上,任何帮助都将是巨大的 import os, json, sys reload(sys) sys.setdefaultencoding('utf-8') path = "G:\Testfolders" def get_list_of_dirs(path):

我有一个脚本,将列出所有文件夹和子文件夹,并创建一个JSON文件。我所要做的只是列出包含名为“地图”或“报告”的子文件夹的文件夹。如果它们包含这些内容,则只会列出父文件夹,因此不会显示“地图”、“报告”。目前仍停留在如何实现这一点上,任何帮助都将是巨大的

import os, json, sys
reload(sys)
sys.setdefaultencoding('utf-8')
path = "G:\Testfolders"

def get_list_of_dirs(path):

    try:
        output_dictonary = {}
        list_of_dirs = [os.path.join(path, item) for item in os.listdir(path) if os.path.isdir(os.path.join(path, item )) and os.path.isdir("./Maps") and os.path.isdir("./Reports")]

        output_dictonary["text"] = path.decode('latin1')
        output_dictonary["type"] = "directory"
        output_dictonary["children"] = []
        for dir in list_of_dirs:
        output_dictonary["children"].append(get_list_of_dirs(dir))
        return output_dictonary
    except WindowsError:
        pass

    print json.dumps(get_list_of_dirs(path))

with open(r'U:\JSONDatatest.json', 'w') as f:
     json.dump(get_list_of_dirs(path), f)
编辑:

import os, json, sys

def all_dirs_with_subdirs(path,subdirs):
    # make sure no relative paths are returned, can be omitted
    try:
        output_dictonary = {}

        path = os.path.abspath(path)

        list_of_dirs = []
        for root, dirs, files in os.walk(path):
            if all(subdir in dirs for subdir in subdirs):
                    list_of_dirs.append(root)
        return list_of_dirs
        output_dictonary["text"] = path.decode('latin1')
        output_dictonary["type"] = "directory"

        output_dictonary["children"] = []
        for dir in list_of_dirs:
            output_dictonary["children"].append(all_dirs_with_subdirs(dir))
        return output_dictonary
    except WindowsError:
        pass

with open(r'U:\jsontesting\JSONData.json', 'w') as f:
     json.dump(all_dirs_with_subdirs("G:\TESTPATH", ('Maps', 'Temp')), f)

我想您正在寻找
os.walk
命令,我在下面使用它实现了一个函数。(也使其更通用)


更新:如果您想使用glob,也可以使用JSON内容

def get_list_of_dirs():

    from glob import glob
    import os.path as path

    # get the directories containing Maps
    maps = set([path.abspath(path.join(f + path.pardir)) 
              for f in glob('**/Maps/')])
    # get the directories containing Reports
    reports = set([path.abspath(path.join(f + path.pardir)) 
                      for f in glob('**/Reports/')])
    # return the common ones
    return maps.intersection(reports)

您可以通过向函数传递glob表达式列表,然后返回集合的交集或边走边交集,使其更通用。

p.S.我知道我没有提到json,我认为用json编码unicode符号数组已经不再是一个问题了,因为这一部分应该可以在atm机上工作。谢谢,这非常有效。但是,JSON在获取该输出列表的父/子树时遇到问题。我试着用它来转换json,但它只是列出了没有子文件夹的文件夹列表等。我已经把它添加到了我的问题中。太棒了。所有子文件夹都被分组为一级下级子文件夹。例如,G:\TEST\a\b\与G:\TEST\a\处于同一级别。有没有办法保留其原始结构?基本上,我使用这个JSON来填充JQuery JSTree。因为它是伟大的作品,除了我有一个数以百计的文件夹列表,所以当用户折叠列表,它填补了页面。需要找到一种保留父子etc结构的方法。我会摆弄它,看看我能不能找到到底发生了什么。看,任何东西都可以以任何方式适应工作,当然,在某种程度上,这从回答你的问题到只是为你做项目。话虽如此,要做类似的事情,您可以使用dict查看目录的父目录是否已经存在,并以某种方式将其存储在不同的数据结构中。祝你好运
def get_list_of_dirs():

    from glob import glob
    import os.path as path

    # get the directories containing Maps
    maps = set([path.abspath(path.join(f + path.pardir)) 
              for f in glob('**/Maps/')])
    # get the directories containing Reports
    reports = set([path.abspath(path.join(f + path.pardir)) 
                      for f in glob('**/Reports/')])
    # return the common ones
    return maps.intersection(reports)