Last active
September 30, 2020 16:03
-
-
Save acro5piano/a6bf1df99f56f340e609d3b1c6484caa to your computer and use it in GitHub Desktop.
あるディレクトリ以下のファイルツリーをJSONで再帰的に出力する ref: https://qiita.com/acro5piano/items/9ddbc2f330c9084833f9
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
#!/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