想递归查找指定目录下全部文件和文件夹,并生成如下格式的字典数据格式
{
"label":"文件夹1",
"children":[
{
"label":"文件1"
},
{
"label":"文件夹2",
"children":[
{"label":"文件2"}
]
}
]
}
哪位好心人帮帮忙,没有C币
#coding:UTF-8
import os
import collections
def search_records_path(path):
list_result = list()
for _dir in os.listdir(path):
result = dict()
file_path = path + "/" + _dir
if os.path.isdir(file_path):
result['label'] = "dir:" + _dir
children = search_records_path(file_path)
result['children'] = children
else:
result['label'] = "file:" + _dir
list_result.insert(0, result)
return list_result
print search_records_path("D:\PythonTest\Test1")
你自己看看对不对,大致差不多,你自己看着修改,对的话请采纳