Created
November 1, 2017 13:35
-
-
Save delivrance/e75955f901098d7e7429593112b5a824 to your computer and use it in GitHub Desktop.
Builds a dictionary representing a directory tree
This file contains 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 json | |
import os | |
def build(path): | |
tree = {} | |
def walk(path, tree): | |
for i in os.listdir(path): | |
tree[i] = {} | |
try: | |
walk("{}/{}".format(path, i), tree[i]) | |
except NotADirectoryError: | |
tree[i] = None | |
walk(path, tree) | |
return tree | |
path = "." | |
tree = build(path) | |
print(json.dumps(tree, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment