因为需要实现一个树形结构图,需要读取本地的某个目录下的文件夹里面的内容,而且文件夹里面可以嵌套任意数量的文件和文件夹,需要把这个读取出来并且显示在网页上。
找到了这个ztree插件,ztree需要文件目录结构的json文件,所以需要将本地的文件夹遍历生成json格式,采用生成的是简单的json格式,类似于
var zNodes = [
{ id: 1, pId: 0, name: "随意勾选 1", open: true},
{ id: 11, pId: 1, name: "随意勾选 1-1", open: true},
{ id: 111, pId: 11, name: "随意勾选 1-1-1"},
{ id: 112, pId: 11, name: "随意勾选 1-1-2"},
{ id: 12, pId: 1, name: "随意勾选 1-2", open: true},
{ id: 121, pId: 12, name: "随意勾选 1-2-1"},
{ id: 122, pId: 12, name: "随意勾选 1-2-2"},
{ id: 2, pId: 0, name: "随意勾选 2", checked: true, open: true},
{ id: 21, pId: 213, name: "随意勾选 21-3"},
{ id: 22, pId: 2, name: "随意勾选 2-2", open: true},
{ id: 221, pId: 22, name: "随意勾选 2-2-1", checked: true},
{ id: 222, pId: 22, name: "随意勾选 2-2-2"},
{ id: 23, pId: 2, name: "随意勾选 2-3"}
];
python遍历文件夹是两种方式:
# -*- coding: utf-8 -*-
import os
def Test1(rootDir):
list_dirs = os.walk(rootDir)
for root, dirs, files in list_dirs:
for d in dirs:
print os.path.join(root, d)
for f in files:
print os.path.join(root, f)
# -*- coding: utf-8 -*-
import os
def Test2(rootDir):
for lists in os.listdir(rootDir):
path = os.path.join(rootDir, lists)
print path
if os.path.isdir(path):
Test2(path)
第一种是先输出当前目录下的所有的文件和文件夹,是一层层向下的,而第二种是每个树遍历完了在遍历新的一个父节点的,类似先序遍历
这里选择第一种方式加以修改,生成所需的json文件
import os
import os.path
import json
p_id = 1
p_pid = 0
jsondict = dict()
jsonlist = list()
dirpath = r"e:\tree" # p_id=1 ,p_pid =0
jsondict = {"id": 1, "pId": 0, "path": r"e:\tree", "name": "tree"}
jsonlist.append(jsondict)
def getnumlength(fnum=0):
numlength = 0
while(fnum > 0):
fnum = fnum / 10
numlength += 1
return numlength
def get_id_pid(root):
for x in jsonlist:
if x["path"] == root:
return x["id"]
def getfile(dirpath):
list_dirs = os.walk(dirpath)
for root, dirs, files in list_dirs:
fnum = 1
global p_id
numlength = getnumlength(fnum)
c_pid = get_id_pid(root)
for d in dirs:
# c_pid = p_id
c_id = c_pid * pow(10, numlength) + fnum
fnum += 1
print os.path.join(root, d), c_pid, c_id, d, root
jsondict = {"id": c_id, "pId": c_pid,
"path": os.path.join(root, d), "name": d}
jsonlist.append(jsondict)
for f in files:
# c_pid = p_id
c_id = c_pid * pow(10, numlength) + fnum
fnum += 1
print os.path.join(root, f), c_pid, c_id, f, root
jsondict = {"id": c_id, "pId": c_pid,
"path": os.path.join(root, d), "name": f}
jsonlist.append(jsondict)
getfile(dirpath)
print json.dumps(jsonlist)
显示效果如图:
因为需要实现一个树形结构图,需要读取本地的某个目录下的文件夹里面的内容,而且文件夹里面可以嵌套任意数量的文件和文件夹,需要把这个读取出来并且显示在网页上。
找到了这个ztree插件,ztree需要文件目录结构的json文件,所以需要将本地的文件夹遍历生成json格式,采用生成的是简单的json格式,类似于
var zNodes = [
{ id: 1, pId: 0, name: "随意勾选 1", open: true},
{ id: 11, pId: 1, name: "随意勾选 1-1", open: true},
{ id: 111, pId: 11, name: "随意勾选 1-1-1"},
{ id: 112, pId: 11, name: "随意勾选 1-1-2"},
{ id: 12, pId: 1, name: "随意勾选 1-2", open: true},
{ id: 121, pId: 12, name: "随意勾选 1-2-1"},
{ id: 122, pId: 12, name: "随意勾选 1-2-2"},
{ id: 2, pId: 0, name: "随意勾选 2", checked: true, open: true},
{ id: 21, pId: 213, name: "随意勾选 21-3"},
{ id: 22, pId: 2, name: "随意勾选 2-2", open: true},
{ id: 221, pId: 22, name: "随意勾选 2-2-1", checked: true},
{ id: 222, pId: 22, name: "随意勾选 2-2-2"},
{ id: 23, pId: 2, name: "随意勾选 2-3"}
];
python遍历文件夹是两种方式:
# -*- coding: utf-8 -*-
import os
def Test1(rootDir):
list_dirs = os.walk(rootDir)
for root, dirs, files in list_dirs:
for d in dirs:
print os.path.join(root, d)
for f in files:
print os.path.join(root, f)
# -*- coding: utf-8 -*-
import os
def Test2(rootDir):
for lists in os.listdir(rootDir):
path = os.path.join(rootDir, lists)
print path
if os.path.isdir(path):
Test2(path)
第一种是先输出当前目录下的所有的文件和文件夹,是一层层向下的,而第二种是每个树遍历完了在遍历新的一个父节点的,类似先序遍历
这里选择第一种方式加以修改,生成所需的json文件
import os
import os.path
import json
p_id = 1
p_pid = 0
jsondict = dict()
jsonlist = list()
dirpath = r"e:\tree" # p_id=1 ,p_pid =0
jsondict = {"id": 1, "pId": 0, "path": r"e:\tree", "name": "tree"}
jsonlist.append(jsondict)
def getnumlength(fnum=0):
numlength = 0
while(fnum > 0):
fnum = fnum / 10
numlength += 1
return numlength
def get_id_pid(root):
for x in jsonlist:
if x["path"] == root:
return x["id"]
def getfile(dirpath):
list_dirs = os.walk(dirpath)
for root, dirs, files in list_dirs:
fnum = 1
global p_id
numlength = getnumlength(fnum)
c_pid = get_id_pid(root)
for d in dirs:
# c_pid = p_id
c_id = c_pid * pow(10, numlength) + fnum
fnum += 1
print os.path.join(root, d), c_pid, c_id, d, root
jsondict = {"id": c_id, "pId": c_pid,
"path": os.path.join(root, d), "name": d}
jsonlist.append(jsondict)
for f in files:
# c_pid = p_id
c_id = c_pid * pow(10, numlength) + fnum
fnum += 1
print os.path.join(root, f), c_pid, c_id, f, root
jsondict = {"id": c_id, "pId": c_pid,
"path": os.path.join(root, d), "name": f}
jsonlist.append(jsondict)
getfile(dirpath)
print json.dumps(jsonlist)
显示效果如图:
![](https://img.haomeiwen.com/i506738/4a6cb4e3ec630080.png)
网友评论