创建文件夹,文件树

时间:2019-12-02 00:23:49

标签: python python-3.x

我有一个像这样的文件夹结构:

pages/
    sidebar.md
    start.md
    tes.md
    test.md
    test/
        tes.md
        test.md
        1/
            tes.md
            test.md
            2/
                tes.md
                test.md
    test1/
        sidebar.md
        start.md
        tes.md
        test.md
        33/
            sidebar.md
            start.md
            tes.md
            test.md
    test2/
        tes.md
        test.md

树从此代码获取:

def showFolderTree(path,indentation=2):
    tree = []

    for root, dirs, files in os.walk(path):
        level = root.replace(path, '').count(os.sep)
        indent = ' '*indentation*(level)
        tree.append('{}{}/'.format(indent,os.path.basename(root))) 
        print('{}{}/'.format(indent,os.path.basename(root)))
        for f in files:
            subindent=' ' * indentation * (level+1)
            tree.append('{}{}'.format(subindent,f))
            print('{}{}'.format(subindent,f))

我如何可以采用以下形式:

  

[{“ id”:1,“ text”:“ pages”,“ children”:[{“ id”:2,“ text”:“ sidebar.md”},{“ id”:3,“ text“:” start.md“},{” id“:4,” text“:” tes“},{” id“:5,” text“:” test.md“},{” id“:6 ,“ text”:“ test”,“ children”:[{“ id”:7,“ text”:“ tes.md”},{“ id”:8,“ text”:“ test.md”}, {“ id”:9,“ text”:“ 1”}]}]}   等

0 个答案:

没有答案