Python list only folders that contain specific subfolders
<p>I have a script that will list all folders and subfolders and create a JSON file.  What I am trying to have is only folders that contain subfolders named "Maps" or "Reports" listed.  If they contain those then only the parent folder will be listed, so  "Maps", "Reports" would not be shown.  Currently stuck on how to accomplish this, any help would be great.</p>

<pre><code>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)
</code></pre>

<p><strong>Edited:</strong></p>

<pre><code>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)
</code></pre>

<p>I'm thinking you're looking for the <code>os.walk</code> command, I've implemented a function using it below. (Also made it slightly more generic)</p>

<pre><code>import os

# path   : string to relative or absolute path to be queried
# subdirs: tuple or list containing all names of subfolders that need to be
#          present in the directory
def all_dirs_with_subdirs(path, subdirs):
    # make sure no relative paths are returned, can be omitted
    path = os.path.abspath(path)

    result = []
    for root, dirs, files in os.walk(path):
        if all(subdir in dirs for subdir in subdirs):
                result.append(root)
    return result

def get_directory_listing(path):
    output = {}
    output["text"] = path.decode('latin1')
    output["type"] = "directory"
    output["children"] = all_dirs_with_subdirs(path, ('Maps', 'Reports'))
    return output

with open('test.json', 'w+') as f:
    listing = get_directory_listing(".")
    json.dump(listing, f)
</code></pre>

<p><strong>UPDATE: Also the JSON stuff</strong></p>

python python-2.7