import os def get_directory_tree(path): tree = {'name': path} if os.path.isdir(path): tree['type'] = 'directory' tree['children'] = [get_directory_tree(os.path.join(path, child)) for child in os.listdir(path)] else: tree['type'] = 'file' return tree