Skip to content

Instantly share code, notes, and snippets.

@anderskristo
Created February 22, 2015 00:24
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 anderskristo/639048f63531c8517b19 to your computer and use it in GitHub Desktop.
Save anderskristo/639048f63531c8517b19 to your computer and use it in GitHub Desktop.
files in directory to json script
#!/usr/bin/env python
import os
import errno
def path_hierarchy(path):
hierarchy = {
'name': os.path.basename(path),
'icon': path,
}
try:
hierarchy['children'] = [
path_hierarchy(os.path.join(path, contents))
for contents in os.listdir(path)
]
except OSError as e:
if e.errno != errno.ENOTDIR:
raise
hierarchy['type'] = 'file'
return hierarchy
if __name__ == '__main__':
import json
import sys
try:
directory = sys.argv[1]
except IndexError:
directory = "."
print(json.dumps(path_hierarchy(directory), indent = 2, sort_keys = True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment