最近遇到一個問題就是某個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)
至於如何返回某路徑下所有子目錄及該路徑下某型別的檔案,不是本文重點也很簡單,不再冗述!