Created
March 18, 2012 19:49
-
-
Save lchi/2080379 to your computer and use it in GitHub Desktop.
Jsonizing your fs
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 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