Skip to content

Instantly share code, notes, and snippets.

@debodirno
Created October 20, 2018 10:42
Show Gist options
  • Save debodirno/18a21df0511775c19de8d7ccbc99cb72 to your computer and use it in GitHub Desktop.
Save debodirno/18a21df0511775c19de8d7ccbc99cb72 to your computer and use it in GitHub Desktop.
Directory Tree in JSON
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