Created
February 22, 2015 00:24
-
-
Save anderskristo/639048f63531c8517b19 to your computer and use it in GitHub Desktop.
files in directory to json script
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
#!/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