最近遇到一个问题就是某个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)

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

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

  1. 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

    原文:将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小) WPF的XAML文档(Main.xaml): <Window x:Class="SVG2Image.Ma ...

  2. C++查找指定路径下的特定类型的文件

    转载:https://www.cnblogs.com/tinaluo/p/6824674.html 例子:找到C盘中所有后缀为exe的文件(不包括文件夹下的exe文件) #include<std ...

  3. 删除指定路径下固定格式,以.log结尾、三天前的文件,或删除空的日志文件

    师出‘百测’besttest 删除指定路径下固定格式,以.log结尾.三天前的文件,或删除空的日志文件. 日志文件格式:XXXX_2019-01-01.log. import os,datetime ...

  4. python之实现循环查看指定路径下的所有文件---os.walk

    循环查看指定路径下的所有文件.文件夹,包含隐藏文件注:“.filename” 以点开头的是隐藏文件 import os for cur_path,cur_dirs,cur_files in os.wa ...

  5. Python获取指定路径下所有文件的绝对路径

    需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...

  6. Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间

    遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间 import osimport datetime def print_tree(dir_path): for ...

  7. Python3在指定路径下递归定位文件中出现的字符串

    [本文出自天外归云的博客园] 脚本功能:在指定的路径下递归搜索,找出指定字符串在文件中出现的位置(行信息). 用到的python特性: 1. PEP 318 -- Decorators for Fun ...

  8. C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名

    1.实现把指定文件夹下的所有文件复制到指定路径下 public static void copyFiles(string path) { DirectoryInfo dir = new Directo ...

  9. java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件

    /** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...

随机推荐

  1. 我是如何利用Hadoop做大规模日志压缩的

    背景 刚毕业那几年有幸进入了当时非常热门的某社交网站,在数据平台部从事大数据开发相关的工作.从日志收集.存储.数据仓库建设.数据统计.数据展示都接触了一遍,比较早的赶上了大数据热这波浪潮.虽然今天的人 ...

  2. php字符的替换,截取,指定查找

    <?php/** * Created by 郭鹏. * User: msi * Date: 2017/9/27 * Time: 14:17 *///随机数生成器echo rand();echo ...

  3. Angular - Templates(模板)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 在Angular中,模板是一个包含了Angular特定元素和属性的HTML.Angula ...

  4. myeclipse快捷键(转载)

    非常感谢分享这篇文章的大虾..但是我忘了几下您的blog地址,因此无法注明原文地址...见谅哈 存盘 Ctrl+s(肯定知道) 注释代码 Ctrl+/ 取消注释 Ctrl+\(Eclipse3已经都合 ...

  5. Linux 安装Anaconda 4.4.0

    安装步骤参考了官网的说明:https://docs.anaconda.com/anaconda/install/linux.html 具体步骤如下:  1.在官网下载地址 https://www.an ...

  6. 【译】Yarn上常驻Spark-Streaming程序调优

    作者从容错.性能等方面优化了长时间运行在yarn上的spark-Streaming作业 对于长时间运行的Spark Streaming作业,一旦提交到YARN群集便需要永久运行,直到有意停止.任何中断 ...

  7. bzoj1968 COMMON 约数研究

    Input只有一行一个整数 N(0 < N < 1000000).Output只有一行输出,为整数M,即f(1)到f(N)的累加和.Sample Input 3 Sample Output ...

  8. 【归纳整理】Ajax / JSON / WEB存储 / iframe

      Ajax 一.什么是 AJAX ? AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 是一种用于创建快速动态网页 ...

  9. Python 获取当前脚本文件路径目录

    # -*- coding: cp936 -*- import sys,os # 获取脚本文件的当前路径 def cur_file_dir(): # 获取脚本路径 path = sys.path[0] ...

  10. HDU3605 Escape

    思想:缩点+sap Max,t还可以缩小,优化,高数课写的,有点丑,暂时懒得改. #include<cstdio> #include<cstdlib> #include< ...