Skip to content

Instantly share code, notes, and snippets.

@lchi
Created March 18, 2012 19:49
Show Gist options
  • Save lchi/2080379 to your computer and use it in GitHub Desktop.
Save lchi/2080379 to your computer and use it in GitHub Desktop.
Jsonizing your fs
import json
import os
import sys
def makeTree(path):
t = {path:{}}
tree(path, t[path])
return t
def tree(path, t):
t['size'] = os.stat(path).st_size
t['children'] = {}
if os.path.isdir(path):
for child in os.listdir(path):
t['children'][child] = {}
for key in t['children']:
nextChild = path + os.sep + key
tree(nextChild, t['children'][key])
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage: python jsonize_fs.py <dir>'
sys.exit(1)
print json.dumps(makeTree(sys.argv[1]), indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment