Is there any easy way to generate such a JSON? I found os.walk()
and os.listdir()
, so I may do recursive descending into directories and build a py
I just had to do this (well, almost) so hit this page but the above doesn't recurse into subdirectories.
So this version only handles directories, not files, but you could add those.
First generate a nested python dict:
def fs_tree(root):
results = {}
for (dirpath, dirnames, filenames) in os.walk(root):
parts = dirpath.split(os.sep)
curr = results
for p in parts:
curr = curr.setdefault(p, {})
return results
Secondly dump that to json using the json
module.