Skip to content

Instantly share code, notes, and snippets.

@acro5piano
Last active September 30, 2020 16:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acro5piano/a6bf1df99f56f340e609d3b1c6484caa to your computer and use it in GitHub Desktop.
Save acro5piano/a6bf1df99f56f340e609d3b1c6484caa to your computer and use it in GitHub Desktop.
あるディレクトリ以下のファイルツリーをJSONで再帰的に出力する ref: https://qiita.com/acro5piano/items/9ddbc2f330c9084833f9
#!/usr/bin/env python
import os
import json
def json_tree(path):
target_json = []
for item in os.listdir(path):
new_hash = {}
new_hash['text'] = item
full_path = os.path.join(path,item)
if os.path.isdir(full_path):
child_dir = json_tree(full_path)
new_hash['nodes'] = append(child_dir)
else:
new_hash['href'] = full_path.replace(os.getcwd(),'')
target_json.append(new_hash)
return target_json
if __name__ == '__main__':
path = os.getcwd()+'/files'
print(json.dumps(json_tree(path)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment