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 ...
随机推荐
- 西邮linux兴趣小组2014纳新免试题(四)
[第四关] 题目 http://findakey.sinaapp.com/ Example: String1:FFFF8 5080D D0807 9CBFC E4A04 24BC6 6C840 49B ...
- spring boot / cloud (十八) 使用docker快速搭建本地环境
spring boot / cloud (十八) 使用docker快速搭建本地环境 在平时的开发中工作中,环境的搭建其实一直都是一个很麻烦的事情 特别是现在,系统越来越复杂,所需要连接的一些中间件也越 ...
- Akka(27): Stream:Use case-Connecting Slick-dbStream & Scalaz-stream-fs2
在以前的博文中我们介绍了Slick,它是一种FRM(Functional Relation Mapper).有别于ORM,FRM的特点是函数式的语法可以支持灵活的对象组合(Query Composit ...
- SVG轨迹回放实践
最近做了埋点方案XTracker的轨迹回放功能,大致效果就是,在指定几个顺序的点之间形成轨迹,来模拟用户在页面上的先后行为(比如一个用户先点了啥,后点了啥).效果图如下: 在这篇文章中,我们来聊聊轨迹 ...
- IFrame父页面和子页面的交互
现在在页面里面用到iframe的情况越来越少了,但有时还是避免不了,甚至这些页面之间还需要用js来做交互,那么这些页面如何操作彼此的dom呢?下面将会逐步介绍. 1.父页面操作子页面里面的dom 下面 ...
- ThinkPHP中使用PHPMailer邮件类
第一步.添加PHPMailer类库将下载后的文件解压,将PHPMail目录移动至ThinkPHP目录中的Vendor内.(请确保class.phpmailer.php文件就在ThinkPHP\Vend ...
- JavaScript 版数据结构与算法(二)队列
今天,我们要讲的是数据结构与算法中的队列. 队列简介 队列是什么?队列是一种先进先出(FIFO)的数据结构.队列有什么用呢?队列通常用来描述算法或生活中的一些先进先出的场景,比如: 在图的广度优先遍历 ...
- Python selenium 文件自动下载 (自动下载器)
MyGithub:https://github.com/williamzxl 最新代码已经上传到Github,以下版本为stupid版本. 由于在下载过程中需要下载不同文件,所以可以把所有类型放在Va ...
- zabbix灵活使用userparameters
userparameters介绍 官网文献:https://www.zabbix.com/documentation/2.0/manual/config/items/userparameters 当我 ...
- IP报文分片
1. 最大传输单元(Maximum Transmission Unit,MTU). 以太网帧中的数据长度规定最小46 字节,最大1500 字节,MTU 指数据帧中有效载荷的最大长度,不包括帧首部的长度 ...