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)
至于如何返回某路径下所有子目录及该路径下某类型的文件,不是本文重点也很简单,不再冗述!
python如何将指定路径下的某类型文件,返回一个树形结构体,让前端显示为树形的目录结构的更多相关文章
- 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
原文:将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小) WPF的XAML文档(Main.xaml): <Window x:Class="SVG2Image.Ma ...
- C++查找指定路径下的特定类型的文件
转载:https://www.cnblogs.com/tinaluo/p/6824674.html 例子:找到C盘中所有后缀为exe的文件(不包括文件夹下的exe文件) #include<std ...
- 删除指定路径下固定格式,以.log结尾、三天前的文件,或删除空的日志文件
师出‘百测’besttest 删除指定路径下固定格式,以.log结尾.三天前的文件,或删除空的日志文件. 日志文件格式:XXXX_2019-01-01.log. import os,datetime ...
- python之实现循环查看指定路径下的所有文件---os.walk
循环查看指定路径下的所有文件.文件夹,包含隐藏文件注:“.filename” 以点开头的是隐藏文件 import os for cur_path,cur_dirs,cur_files in os.wa ...
- Python获取指定路径下所有文件的绝对路径
需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...
- Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间
遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间 import osimport datetime def print_tree(dir_path): for ...
- Python3在指定路径下递归定位文件中出现的字符串
[本文出自天外归云的博客园] 脚本功能:在指定的路径下递归搜索,找出指定字符串在文件中出现的位置(行信息). 用到的python特性: 1. PEP 318 -- Decorators for Fun ...
- C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名
1.实现把指定文件夹下的所有文件复制到指定路径下 public static void copyFiles(string path) { DirectoryInfo dir = new Directo ...
- java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件
/** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...
随机推荐
- 走进Node.js 之 HTTP实现分析
作者:正龙(沪江Web前端开发工程师) 本文为原创文章,转载请注明作者及出处 上文"走进Node.js启动过程"中我们算是成功入门了.既然Node.js的强项是处理网络请求,那我们 ...
- 树状数组初步_ZERO
原博客:树状数组 1 一维树状数组 1 什么是树状数组 树状数组是一个查询和修改复杂度都为log(n)的数据结构,假设数组A[1..n],那么查询A[1]+-+A[n]的时,间是log级 ...
- Postman 串行传参和动态传参详解
Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件 用Postman做接口测试的时候,要把多条用例一起执行,就需要把用例连接起来,一次性执行 目录 串行传参 动态传参 使用 ...
- 【转】深入理解CSS定位中的偏移
前面的话 CSS有三种基本的定位机制:普通流.浮动和绝对定位.利用定位,可以准确地定义元素框相对于其正常位置应该出现的位置,或者相对于父元素.另一个元素甚至浏览器窗口本身的位置.但元素究竟如何定位,定 ...
- mysql error 1130 hy000:Host 'localhost' is not allowed to connect to this mysql server 解决方案
ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to this MySQL server D:\Wamp\mysql-\b ...
- k-选取问题
一.k-选取问题:给定任意一个可比较的序列,从中找出第k个元素(k从0开始,默认是从小到大的次序)的问题称为k-选取(k-selection).k-选取问题有两张退化的情况:1.0-选取问题即是找出序 ...
- 有人提了一个问题:一定要RESTful吗?
写在前面的话 这个问题看起来就显得有些萌,或者说类似的问题都有些不靠谱,世上哪有那么多一定的事情,做开发都不一定做多久呢,所以说如果你有这个疑问的话是真真有点儿不着调,不过可能也就是随口一问吧,没有深 ...
- 微信小程序城市定位(借助百度地图API判断城市)
概述 微信小程序提供一些API(地址)用于获取当前用户的地理位置等信息,但无论是wx.getLocation,还是wx.chooseLocation均没有单独的字段表示国家与城市信息,仅有经纬度信息. ...
- UVa10791 - Minimum Sum LCM
分析即为紫薯上的分析. 难点是发现当每个aipi作为一个单独的整数时才最优.. 答案就是将所有不同的 相同因子的积 相加即可 代码: #include<cstdio> #include&l ...
- Java 集合框架之set用法
Java 集合框架之set 一个简单的例子 创建一个Customer类,类中的属性有姓名(name).年龄(age).性别(gender),每个属性分别有get/set 方法.然后创建两个Custom ...