ztree與python遍歷文件夾

因?yàn)樾枰獙?shí)現(xiàn)一個(gè)樹(shù)形結(jié)構(gòu)圖,需要讀取本地的某個(gè)目錄下的文件夾里面的內(nèi)容,而且文件夾里面可以嵌套任意數(shù)量的文件和文件夾,需要把這個(gè)讀取出來(lái)并且顯示在網(wǎng)頁(yè)上。
找到了這個(gè)ztree插件,ztree需要文件目錄結(jié)構(gòu)的json文件,所以需要將本地的文件夾遍歷生成json格式,采用生成的是簡(jiǎn)單的json格式,類(lèi)似于

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è)樹(shù)遍歷完了在遍歷新的一個(gè)父節(jié)點(diǎn)的,類(lèi)似先序遍歷

這里選擇第一種方式加以修改,生成所需的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è)樹(shù)形結(jié)構(gòu)圖,需要讀取本地的某個(gè)目錄下的文件夾里面的內(nèi)容,而且文件夾里面可以嵌套任意數(shù)量的文件和文件夾,需要把這個(gè)讀取出來(lái)并且顯示在網(wǎng)頁(yè)上。
找到了這個(gè)ztree插件,ztree需要文件目錄結(jié)構(gòu)的json文件,所以需要將本地的文件夾遍歷生成json格式,采用生成的是簡(jiǎn)單的json格式,類(lèi)似于

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è)樹(shù)遍歷完了在遍歷新的一個(gè)父節(jié)點(diǎn)的,類(lèi)似先序遍歷

這里選擇第一種方式加以修改,生成所需的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-checkbox.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容