#!/usr/bin/python # created by Roman Plevka # this script takes a path as an argument and outputs its structure as a valid JSON object # currently used attributes: type (file|dir), name (String), size (Number) [, children (Array of Obj)] # Change history: # 2014-07-16 by Roman created # 2014-09-03 by Roman algorithm (fixed for links - no more links following) # 2014-09-09 by Roman method (added extra parameters for specifying the output file) # output (added extra attribute to the root level - date&time [dttm]) # 2014-09-29 by Roman algorithm (get real inode number) # 2014-11-01 by Roman output (data wrapped in the root node) # 2014-11-07 by Roman output (3rd argument implemented - "nofiles" - for output to ommit type:files) # 2014-11-08 by Roman output (flag:all|nofiles added) import os, sys, time path = sys.argv[1] output = sys.argv[2] files = sys.argv[3] date = time.strftime("%Y-%m-%d %H:%M:%S") flag = "all" if files == "nofiles": flag = "nofiles" def getFiles(path): first = True; for item in os.listdir(path): print(item) if os.path.islink(os.path.join(path, item)): if files != "nofiles": if first: first = False else: myfile.write(", "); getInfo(path, item) elif os.path.isdir(os.path.join(path, item)): if first: first = False else: myfile.write(", "); fid = os.stat(os.path.join(path, item)).st_ino size = str(totalSize(path+"/"+item)) myfile.write("{ \"id\": "+str(fid)+", \"type\": \"dir\", \"name\": \""+item+"\", \"size\": "+size+", \"children\": ["), getFiles(path+"/"+item) myfile.write("]}"), else: if files != "nofiles": getInfo(path, item) def getInfo(path, f): if os.path.islink(os.path.join(path, f)): size = "0" fid = os.lstat(os.path.join(path, f)).st_ino else: size = str(os.path.getsize(path+"/"+f)) fid = os.stat(os.path.join(path, f)).st_ino myfile.write("{ \"id\": "+str(fid)+", \"type\": \"file\", \"name\": \""+f+"\", \"size\": "+size+" }"), def totalSize(source): total_size = os.path.getsize(source) for item in os.listdir(source): itempath = os.path.join(source, item) if os.path.islink(itempath): total_size += 17 elif os.path.isfile(itempath): total_size += os.path.getsize(itempath) elif os.path.isdir(itempath): total_size += totalSize(itempath) return total_size with open(output,'w') as myfile: myfile.write("{\"dttm\": \""+date+"\", \"flag\": \""+flag+"\", \"data\": [") rfid = os.stat(path).st_ino rsize = str(totalSize(path)) ritems = path.split("/"); ritem = str(ritems[len(ritems)-1]) myfile.write("{ \"id\": "+str(rfid)+", \"type\": \"dir\", \"name\": \""+ritem+"\", \"size\": "+rsize+", \"children\": ["), getFiles(path) myfile.write("]}]}")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question