-
-
Save debodirno/18a21df0511775c19de8d7ccbc99cb72 to your computer and use it in GitHub Desktop.
Directory Tree in JSON
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 os | |
import sys | |
import json | |
def tree_path_json(path): | |
dir_structure = {} | |
base_name = os.path.basename(os.path.realpath(path)) | |
if os.path.isdir(path): | |
dir_structure[base_name] = [ tree_path_json(os.path.join(path, file_name))\ | |
for file_name in os.listdir(path) ] | |
else: | |
return os.path.basename(path) | |
return dir_structure | |
if len(sys.argv) > 1: | |
path = sys.argv[1] | |
else: | |
path = '.' | |
print json.dumps(tree_path_json(path), indent = 4, separators = (', ', ' : ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment