因?yàn)樾枰獙?shí)現(xiàn)一個(gè)樹形結(jié)構(gòu)圖,需要讀取本地的某個(gè)目錄下的文件夾里面的內(nèi)容坛善,而且文件夾里面可以嵌套任意數(shù)量的文件和文件夾,需要把這個(gè)讀取出來并且顯示在網(wǎng)頁(yè)上。
找到了這個(gè)ztree插件,ztree需要文件目錄結(jié)構(gòu)的json文件季惯,所以需要將本地的文件夾遍歷生成json格式吠各,采用生成的是簡(jiǎn)單的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)
第一種是先輸出當(dāng)前目錄下的所有的文件和文件夾勉抓,是一層層向下的贾漏,而第二種是每個(gè)樹遍歷完了在遍歷新的一個(gè)父節(jié)點(diǎn)的,類似先序遍歷
這里選擇第一種方式加以修改藕筋,生成所需的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)
顯示效果如圖:
因?yàn)樾枰獙?shí)現(xiàn)一個(gè)樹形結(jié)構(gòu)圖纵散,需要讀取本地的某個(gè)目錄下的文件夾里面的內(nèi)容,而且文件夾里面可以嵌套任意數(shù)量的文件和文件夾念逞,需要把這個(gè)讀取出來并且顯示在網(wǎng)頁(yè)上困食。
找到了這個(gè)ztree插件,ztree需要文件目錄結(jié)構(gòu)的json文件,所以需要將本地的文件夾遍歷生成json格式翎承,采用生成的是簡(jiǎn)單的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)
第一種是先輸出當(dāng)前目錄下的所有的文件和文件夾,是一層層向下的叨咖,而第二種是每個(gè)樹遍歷完了在遍歷新的一個(gè)父節(jié)點(diǎn)的瘩例,類似先序遍歷
這里選擇第一種方式加以修改,生成所需的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)
顯示效果如圖: