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

 #!/usr/bin/python
# encoding: utf-8 def scan_folder(kml_path,root_path): first_folder=[]
second_folder=[]
third_folder=[]
four_folder=[]
fif_folder=[] all_tree=[]
for each_kml in kml_path:
folder_kml=each_kml.replace(root_path,"").strip("/").split("/")
folder_kml_len=len(folder_kml)
if folder_kml_len==1:
if str(folder_kml[0]) not in first_folder:
first_folder.append(str(folder_kml[0]))
elif folder_kml_len==2:
if str(folder_kml[0]) not in first_folder:
first_folder.append(str(folder_kml[0]))
sec=str(folder_kml[0])+"/"+str(folder_kml[1])
if sec not in second_folder:
second_folder.append(sec) elif folder_kml_len==3:
if str(folder_kml[0]) not in first_folder:
first_folder.append(str(folder_kml[0])) sec=str(folder_kml[0])+"/"+str(folder_kml[1])
if sec not in second_folder:
second_folder.append(sec)
thir=str(folder_kml[0])+"/"+str(folder_kml[1])+"/"+str(folder_kml[2])
if thir not in third_folder :
third_folder.append(thir)
elif folder_kml_len==4:
if str(folder_kml[0]) not in first_folder:
first_folder.append(str(folder_kml[0]))
sec=str(folder_kml[0])+"/"+str(folder_kml[1])
if sec not in second_folder:
second_folder.append(sec)
thir=str(folder_kml[0])+"/"+str(folder_kml[1])+"/"+str(folder_kml[2])
if thir not in third_folder :
third_folder.append(thir)
four=str(folder_kml[0])+"/"+str(folder_kml[1])+"/"+str(folder_kml[2])+"/"+str(folder_kml[3])
if four not in four_folder:
four_folder.append(four)
tree=[]
for first in first_folder:
tmp_object={"title":first}
tree.append(tmp_object)
for second in second_folder:
for fi_folder in tree:
if fi_folder["title"]==second.split("/")[0]:
try:
tree[tree.index(fi_folder)]["children"].append({"title":second.split("/")[1]})
except:
tree[tree.index(fi_folder)]["children"]=[]
tree[tree.index(fi_folder)]["children"].append({"title":second.split("/")[1]})
#print tree for third in third_folder:
for fi_folder in tree:
if fi_folder["title"]==third.split("/")[0]:
first_step=tree.index(fi_folder)
for sec_folder in tree[first_step]["children"]:
if sec_folder["title"]==third.split("/")[1]:
try:
tree[first_step]["children"][tree[first_step]["children"].index(sec_folder)]["children"].append({"title":third.split("/")[2]})
except:
tree[first_step]["children"][tree[first_step]["children"].index(sec_folder)]["children"]=[]
tree[first_step]["children"][tree[first_step]["children"].index(sec_folder)]["children"].append({"title":third.split("/")[2]}) for forth in four_folder:
for fi_folder in tree:
if fi_folder["title"]==forth.split("/")[0]:
first_step=tree.index(fi_folder)
for sec_folder in tree[first_step]["children"]:
if sec_folder["title"]==forth.split("/")[1]:
sec_step=tree[first_step]["children"].index(sec_folder)
for thir_folder in tree[first_step]["children"][sec_step]["children"]:
if thir_folder["title"]==forth.split("/")[2]:
try:
tree[first_step]["children"][sec_step]["children"][tree[first_step]["children"][sec_step]["children"].index(thir_folder)]["children"].append({"title":forth.split("/")[3]})
except:
tree[first_step]["children"][sec_step]["children"][tree[first_step]["children"][sec_step]["children"].index(thir_folder)]["children"]=[]
tree[first_step]["children"][sec_step]["children"][tree[first_step]["children"][sec_step]["children"].index(thir_folder)]["children"].append({"title":forth.split("/")[3]})
return tree if __name__=="__main__":
kml_path=["/dir1/dir2/dir6/2.kml","/dir1/dir2/dir5/1.kml","/dir1/dir2/dir3/dir7/1.kml","/dir1/dir2/dir3/dir8/1.kml"]
root_path="/dir1/"
print scan_folder(kml_path,root_path)

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

上一篇:使用bat快速创建cocos2d-x模板


下一篇:【Lua】关于遍历指定路径下所有目录及文件