生成文件夹树状图
Posted dacui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成文件夹树状图相关的知识,希望对你有一定的参考价值。
import os import os.path BRANCH = ‘├─‘ LAST_BRANCH = ‘└─‘ TAB = ‘│ ‘ EMPTY_TAB = ‘ ‘ def get_dir_list(path, placeholder=‘‘): folder_list = [folder for folder in os.listdir(path) if os.path.isdir(os.path.join(path, folder))] file_list = [file for file in os.listdir(path) if os.path.isfile(os.path.join(path, file))] result = ‘‘ for folder in folder_list[:-1]: result += placeholder + BRANCH + folder + ‘ ‘ result += get_dir_list(os.path.join(path, folder), placeholder + TAB) if folder_list: result += placeholder + (BRANCH if file_list else LAST_BRANCH) + folder_list[-1] + ‘ ‘ result += get_dir_list(os.path.join(path, folder_list[-1]), placeholder + (TAB if file_list else EMPTY_TAB)) for file in file_list[:-1]: result += placeholder + BRANCH + file + ‘ ‘ if file_list: result += placeholder + LAST_BRANCH + file_list[-1] + ‘ ‘ return result if __name__ == ‘__main__‘: print(os.path.dirname(os.getcwd())) print(get_dir_list(os.path.dirname(os.getcwd())))
以上是关于生成文件夹树状图的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript:基于任意JSON动态生成树状图(动态生成vue-giant-tree所需idpid)