Last active
May 6, 2017 17:32
-
-
Save robinsingh1/22239525b98cf6d22d5cddb32e50755e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
def get_directory_structure(rootdir): | |
""" | |
Creates a nested dictionary that represents the folder structure of rootdir | |
""" | |
dir = {} | |
rootdir = rootdir.rstrip(os.sep) | |
start = rootdir.rfind(os.sep) + 1 | |
for path, dirs, files in os.walk(rootdir): | |
folders = path[start:].split(os.sep) | |
subdir = dict.fromkeys(files) | |
parent = reduce(dict.get, folders[:-1], dir) | |
parent[folders[-1]] = subdir | |
return dir | |
print json.dumps(get_directory_structure('.')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment