How to parse a directory structure into dictionary?

Related searches

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': 
                        {'f':[{'g':None, {'h':None}, {'i':None}]}

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



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


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

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

Create a nested dictionary from os.walk � Python recipes , Creates a nested dictionary that represents a folder structure. 1 for path, dirs, files in os.walk(rootdir): folders = path[start:].split(os.sep) subdir� The dictionary is Python’s built-in mapping type. Dictionaries map keys to values, making key-value pairs that can then store data. In this tutorial, we will go over the dictionary data structure in Python.

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.                            

    >>> t = {}                                                                    
    >>> set_leaf(t, ['b1','b2','b3'], 'new_leaf')                                 
    >>> print t                                                                   
    {'b1': {'b2': {'b3': 'new_leaf'}}}                                             
    if len(branches) == 1:
        tree[branches[0]] = leaf
    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:'

Python Tutorial: Traversing directories recursively, import os for dirpath, dirs, files in os.walk("./TREE/"): for filename in files: fname = os.path.join(dirpath,filename) with open(fname) as myfile: print( In this step-by-step tutorial, you'll take a deep dive into how to iterate through a dictionary in Python. Dictionaries are a fundamental data structure, and you'll be able to solve a wide variety of programming problems by iterating through them.

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.

Python : How to get list of files in directory and sub directories , For the given path, get the List of all files in the directory tree. ''' def getListOfFiles( dirName): # create a list of file and sub directories. # names in� Parse key/value of a Generic dictionary in C#. Dictionary<key,value> is a useful data structure specially for mapping purposes. After initializing the Dictionary<key,value>, to retrieve the value of a related key, it is necessary to do a lookup of the key, for example, monthPositionMapping["January"]; in the below code.

Listing out directories and files in Python, The following is a list of some of the important methods/functions in Python with tuple, dictionary, set). listdir() – It is used to list the directory contents. The path� Wednesday, February 06, 2013 - 10:24:18 AM - snorkel: Back To Top: You should probably point out that this method is going to consume a large amount of system memory if ran on a large directory, which if you find yourself writing a script to parse files in a directory, chances are you doing it because the directory is huge.

Second, we need to take that structure and turn it into JSON. (I wasn't entirely sure from your question which serializer you wanted to use, so for this answer I will assume that Json.Net is OK.) For the first part, I would create a Dir class which has a name, a dictionary of child directories (for easy lookup) and a set of files.

I have a string which is of the form dictionary for sure. I need to parse it and store it as a dictionary in python. What i have tried is this: myObj={} tmp="" if ':' in line: key,value = line.

  • This structure seems to be quite inconsistent: If there is no subdirectory, the value is None. If there is one subdirectory, the value is a dictionary. If there are more than one subdirectories, the value is a list. And all occurring dictionaries only have a single item. A nested dictionary without any lists and Nones would seem far more appropriate.
  • Wow. Great. I was thinking of a recursive solution. but the setdefault() do the trick better.