python如何将指定路径下的某类型文件,返回一个树形结构体,让前端显示为树形的目录结构

最近遇到一个问题就是某个linux的目录下有各种文件现在的要求是只需要返回.kml格式的文件,并根据前端要求返回如下结构体即:[{'children': [{'children': [{'title': '2.kml'}], 'title': 'dir6'}, {'children': [{'title': '1.kml'}], 'title': 'dir5'}, {'children': [{'children': [{'title': '1.kml'}], 'title': 'dir7'}, {'children': [{'title': '1.kml'}], 'title': 'dir8'}], 'title': 'dir3'}], 'title': 'dir2'}]

前端zui框架需要这样的结构体就可以显示成树形的目录结构,不过目前实现的程序只支持某路径往下带三层目录深度,因而程序并不完美,贴出源代码希望广大网友使用递归等算法实现多层深度的目录结构,同时也相信大家一定会用到这个算法,欢迎大家研究该算法借鉴该算法:

 1 #!/usr/bin/python  
 2 # encoding: utf-8
 3 
 4 def scan_folder(kml_path,root_path):
 5 
 6     first_folder=[]
 7     second_folder=[]
 8     third_folder=[]
 9     four_folder=[]
10     fif_folder=[]
11 
12     all_tree=[]
13     for each_kml in kml_path:
14         folder_kml=each_kml.replace(root_path,"").strip("/").split("/")
15         folder_kml_len=len(folder_kml)
16         if folder_kml_len==1:
17             if str(folder_kml[0]) not in first_folder:
18                 first_folder.append(str(folder_kml[0]))
19         elif folder_kml_len==2:
20             if str(folder_kml[0]) not in first_folder:
21                 first_folder.append(str(folder_kml[0]))
22             sec=str(folder_kml[0])+"/"+str(folder_kml[1])
23             if  sec not in second_folder:
24                 second_folder.append(sec)
25 
26         elif folder_kml_len==3:
27             if str(folder_kml[0]) not in first_folder:
28                 first_folder.append(str(folder_kml[0]))
29             
30             sec=str(folder_kml[0])+"/"+str(folder_kml[1])
31             if  sec not in second_folder:
32                 second_folder.append(sec)
33             thir=str(folder_kml[0])+"/"+str(folder_kml[1])+"/"+str(folder_kml[2])
34             if  thir not in third_folder :
35                 third_folder.append(thir)
36         elif folder_kml_len==4:
37             if str(folder_kml[0]) not in first_folder:
38                 first_folder.append(str(folder_kml[0]))
39             sec=str(folder_kml[0])+"/"+str(folder_kml[1])
40             if  sec not in second_folder:
41                 second_folder.append(sec)
42             thir=str(folder_kml[0])+"/"+str(folder_kml[1])+"/"+str(folder_kml[2])
43             if  thir not in third_folder :
44                 third_folder.append(thir)
45             four=str(folder_kml[0])+"/"+str(folder_kml[1])+"/"+str(folder_kml[2])+"/"+str(folder_kml[3])
46             if four not in four_folder:
47                 four_folder.append(four)
48     tree=[]
49     for first in first_folder:
50         tmp_object={"title":first}
51         tree.append(tmp_object)
52     for second in second_folder:
53         for fi_folder in tree:
54             if fi_folder["title"]==second.split("/")[0]:
55                 try:
56                     tree[tree.index(fi_folder)]["children"].append({"title":second.split("/")[1]})
57                 except:
58                     tree[tree.index(fi_folder)]["children"]=[]
59                     tree[tree.index(fi_folder)]["children"].append({"title":second.split("/")[1]})
60     #print tree
61 
62     for third in third_folder:
63         for fi_folder in tree:
64             if fi_folder["title"]==third.split("/")[0]:
65                 first_step=tree.index(fi_folder)
66                 for sec_folder in tree[first_step]["children"]:
67                     if sec_folder["title"]==third.split("/")[1]:
68                         try:
69                             tree[first_step]["children"][tree[first_step]["children"].index(sec_folder)]["children"].append({"title":third.split("/")[2]})
70                         except:
71                             tree[first_step]["children"][tree[first_step]["children"].index(sec_folder)]["children"]=[]
72                             tree[first_step]["children"][tree[first_step]["children"].index(sec_folder)]["children"].append({"title":third.split("/")[2]})
73 
74 
75 
76     for forth in four_folder:
77        for fi_folder in tree:
78            if fi_folder["title"]==forth.split("/")[0]:
79                first_step=tree.index(fi_folder)
80                for sec_folder in tree[first_step]["children"]:
81                    if sec_folder["title"]==forth.split("/")[1]:
82                        sec_step=tree[first_step]["children"].index(sec_folder)
83                        for thir_folder in tree[first_step]["children"][sec_step]["children"]:
84                            if thir_folder["title"]==forth.split("/")[2]:
85                                try:
86                                     tree[first_step]["children"][sec_step]["children"][tree[first_step]["children"][sec_step]["children"].index(thir_folder)]["children"].append({"title":forth.split("/")[3]})
87                                except:
88                                     tree[first_step]["children"][sec_step]["children"][tree[first_step]["children"][sec_step]["children"].index(thir_folder)]["children"]=[]
89                                     tree[first_step]["children"][sec_step]["children"][tree[first_step]["children"][sec_step]["children"].index(thir_folder)]["children"].append({"title":forth.split("/")[3]})
90     return tree
91                    
92                 
93                                                                     
94       
95 
96 if __name__=="__main__":
97     kml_path=["/dir1/dir2/dir6/2.kml","/dir1/dir2/dir5/1.kml","/dir1/dir2/dir3/dir7/1.kml","/dir1/dir2/dir3/dir8/1.kml"]
98     root_path="/dir1/"
99     print scan_folder(kml_path,root_path)

至于如何返回某路径下所有子目录及该路径下某类型的文件,不是本文重点也很简单,不再冗述!

原文地址:https://www.cnblogs.com/wc554303896/p/7787551.html