我正在尝试为jstree创建一个JSON文件。但是我很难让这段代码输出文件夹的完整路径而只显示文件夹。我是Python新手,如果有任何见解,我将不胜感激!在
目标是让用户选择一个文件夹并在JSTree中返回该文件夹的完整路径。(不在本规范中)。在import os
import json
def path_to_dict(path):
d = {'text': os.path.basename(path)}
if os.path.isdir(path):
d['type'] = "directory"
for root, directories, filenames in os.walk('U:\PROJECTS\MXD_to_PDF'):
for directory in directories:
d['path']= os.path.join(root, directory)
d['children'] = [path_to_dict(os.path.join(path,x)) for x in os.listdir\
(path)]
else:
d['type'] = "file"
#del d["type"]
return d
print json.dumps(path_to_dict('U:\PROJECTS\MXD_to_PDF\TEST'))
with open('U:\PROJECTS\MXD_to_PDF\TEST\JSONData.json', 'w') as f:
json.dump(path_to_dict('U:\PROJECTS\MXD_to_PDF\TEST'), f)
输出:
^{pr2}$
}