Python-遍历文件目录返回字典树【使用layui样式】

import os,json

# HTML模板
html = """


    
      
      Layui
      
      
      
      
      
    
    
        
基本树
    """
    def getDirectoryTree(folder): ''' 遍历本地目录返回字典树 :param folder:文件目录 :return:目录的字典 ''' dirtree = {'children':[]} if os.path.isfile(folder): return {'name':os.path.basename(folder),'href':os.path.abspath(folder)} else: dirtree['name'] = os.path.basename(folder) for item in os.listdir(folder): dirtree['spread'] = True dirtree['children'].append(getDirectoryTree(os.path.join(folder,item))) return dirtree def getDirectoryTreeWithJson(folder): ''' 把字典树转换为字符串并返回 :param folder: 文件夹 :return:字符串 ''' return json.dumps(getDirectoryTree(folder)) info_dict = getDirectoryTreeWithJson(u"F:\\Desktop") result = html.replace("info_dict",info_dict) # print(info_dict) # print(result) # 可以自行格式化 with open("rsp.html","w",encoding = "utf-8")as f: f.write(result) # 在线Json解码和格式化:https://www.5axxw.com/tools/v2/fjson.html

    https://ed-service.com/
    Python-遍历文件目录返回字典树【使用layui样式】_第1张图片

    你可能感兴趣的:(Python-遍历文件目录返回字典树【使用layui样式】)