Skip to content

Instantly share code, notes, and snippets.

@delivrance
Created November 1, 2017 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delivrance/e75955f901098d7e7429593112b5a824 to your computer and use it in GitHub Desktop.
Save delivrance/e75955f901098d7e7429593112b5a824 to your computer and use it in GitHub Desktop.
Builds a dictionary representing a directory tree
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