How to parse a directory structure into dictionary

2020-02-12 16:40发布

I have list of directory structure such as:

['/a/b', '/a/b/c', '/a/b/c/d', '/a/b/c/e', '/a/b/c/f/g', '/a/b/c/f/h', '/a/b/c/f/i']

I want to convert it into dict like a tree structure.

{'/': {'a': {'b': {'c': 
                       [{'d':None}, 
                        {'e':None}, 
                        {'f':[{'g':None, {'h':None}, {'i':None}]}
                       ]
                  }
             }
      }
}

I got stuck where to strat ? Which data structure will be suitable?

Thanks.

标签: python
3条回答
趁早两清
2楼-- · 2020-02-12 17:11

As Sven Marnach said, the output data structure should be more consistent, eg only nested dictionaries where folders are associated to dict and files to None.

Here is a script which uses os.walk. It does not take a list as input but should do what you want in the end if you want to parse files.

import os 
from pprint import pprint

def set_leaf(tree, branches, leaf):
    """ Set a terminal element to *leaf* within nested dictionaries.              
    *branches* defines the path through dictionnaries.                            

    Example:                                                                      
    >>> t = {}                                                                    
    >>> set_leaf(t, ['b1','b2','b3'], 'new_leaf')                                 
    >>> print t                                                                   
    {'b1': {'b2': {'b3': 'new_leaf'}}}                                             
    """
    if len(branches) == 1:
        tree[branches[0]] = leaf
        return
    if not tree.has_key(branches[0]):
        tree[branches[0]] = {}
    set_leaf(tree[branches[0]], branches[1:], leaf)

startpath = '.'
tree = {}
for root, dirs, files in os.walk(startpath):
    branches = [startpath]
    if root != startpath:
        branches.extend(os.path.relpath(root, startpath).split('/'))

    set_leaf(tree, branches, dict([(d,{}) for d in dirs]+ \
                                  [(f,None) for f in files]))

print 'tree:'
pprint(tree)
查看更多
We Are One
3楼-- · 2020-02-12 17:12

basically

lst = ['/a/b', '/a/b/c', '/a/b/c/d', '/a/b/c/e', '/a/b/c/f/g', '/a/b/c/f/h', '/a/b/c/f/i']
dct = {}

for item in lst:
    p = dct
    for x in item.split('/'):
        p = p.setdefault(x, {})

print dct

produces

 {'': {'a': {'b': {'c': {'e': {}, 'd': {}, 'f': {'i': {}, 'h': {}, 'g': {}}}}}}}

this is not exactly your structure, but should give you a basic idea.

查看更多
Fickle 薄情
4楼-- · 2020-02-12 17:12

Start by looking at os.listdir or os.walk. They will allow you to traverse directories recursively. Either automatically (os.walk) or semi-automatically (with os.listdir). You could then store what you find in a dictionary.

查看更多
登录 后发表回答