添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Join Private Q&A

Setup a private space for you and your coworkers to ask questions and share information. Learn more about Teams

I am trying to create a JSON file for jstree. But I'm having trouble getting this code to output the full path of the folders and only show folders. I am new to Python and would appreciate any insight!

The goal is to have users select a folder and bring back the full path of that folder in JSTree. (Not in this code).

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)

Output:

"text": "TEST" , "type": "directory" , "children": [{ "text": "JSONData.json" , "type": "file" "text": "Maps" , "type": "directory" , "children": [{ "text": "MAY24MODIFIED.mxd" , "type": "file" "text": "MAY24MODIFIED 2016-05-24 16.16.16.pdf" , "type": "file" "text": "testst" , "type": "directory" , "children": [] , "path": "U:\\PROJECTS\\MXD_to_PDF\\TEST2\\Maps\\exported" , "path": "U:\\PROJECTS\\MXD_to_PDF\\TEST2\\Maps\\exported" , "path": "U:\\PROJECTS\\MXD_to_PDF\\TEST2\\Maps\\exported"

For me the following solution works: (you wanted only directories)

def get_list_of_dirs(path):
    output_dictonary = {}
    list_of_dirs = [os.path.join(path, item) for item in os.listdir(path) if os.path.isdir(os.path.join(path, item))]
    output_dictonary["text"] = path
    output_dictonary["type"] = "directory"
    output_dictonary["children"] = []
    for dir in list_of_dirs:
        output_dictonary["children"].append(get_list_of_dirs(dir))
    return output_dictonary
print(json.dumps(get_list_of_dirs(path)))

(You can insert the import, your path, and saving to file as you wish)

Is there anyway to not have the double back slashes? "U:\\PROJECTS\\MXD_to_PDF\\TEST2" I see we can add codecs.escape_decode but not sure where to place this? – Infinity8 Aug 24 '16 at 20:30 That's plattform specific, on windows you have something like you write. I see two options there: 1. you can play with os.sep to replace it with something you want (docs.python.org/2/library/os.html) 2. Since at the end the path is just a string, you can use d["text"].replace("\\", "\") – phev8 Aug 24 '16 at 20:49

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.7.26.34458