Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
545 B
7
Indexable
Never
import os
import sys
import json


def tree_path_json(path):
    dir_structure = {}
    base_name = os.path.basename(os.path.realpath(path))
    if os.path.isdir(path):
        dir_structure[base_name] = [tree_path_json(os.path.join(path, file_name)) \
                                    for file_name in os.listdir(path)]
    else:
        return os.path.basename(path)
    return dir_structure


if len(sys.argv) > 1:
    path = sys.argv[1]
else:
    path = 'data/'

print(json.dumps(tree_path_json(path), indent=4, separators=(', ', ' : ')))