python etree遍历_ztree与python遍历文件夹

因为需要实现一个树形结构图,需要读取本地的某个目录下的文件夹里面的内容,而且文件夹里面可以嵌套任意数量的文件和文件夹,需要把这个读取出来并且显示在网页上。

找到了这个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)

显示效果如图:

ztree-checkbox.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值