Reputation: 2418
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.
Upvotes: 10
Views: 14497
Reputation: 188
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)
Upvotes: 7
Reputation: 214959
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.
Upvotes: 11
Reputation: 9948
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.
Upvotes: 1