Python: Create File Directories based on Dictionary Key names
Question: I wonder if there is a way to turn a dictionary into a directory structure.
For a example a dictionary with following keys:

    
    
    dict['dir1']['subdir1']['subsubdir']['folder1']
                ['subdir2']['subsubdir']['folder1']['folder2']['folder3']
                ['subdir3']['subsubdir']
    

would result to a three directories (where each subdirectory correspond to
dictionary key name:

    
    
    /dir1/subdir1/subsubdir/folder1/
    /dir1/subdir2/subsubdir/folder1/folder2/folder3/
    /dir1/subdir3/subsubdir/
    


Answer: 
    from collections import defaultdict
    import os
    import os.path
    
    def tree():
        return defaultdict(tree)
    d = tree()
    
    d["dir1"]["subdir1"]["subsubdir"]["folder1"]
    d["dir1"]["subdir2"]["subsubdir"]["folder1"]["folder2"]["folder3"]
    d["dir1"]["subdir3"]["subsubdir"]
    
    def rec(directory, current_path):
        if len(directory):
            for direc in directory:
                rec(directory[direc], os.path.join(current_path, direc))
        else:
            os.makedirs(current_path)
    
    rec(d, "")
    

**Output**

    
    
    ~/Desktop$ tree dir1
    dir1
    |-- subdir1
    |   `-- subsubdir
    |       `-- folder1
    |-- subdir2
    |   `-- subsubdir
    |       `-- folder1
    |           `-- folder2
    |               `-- folder3
    `-- subdir3
        `-- subsubdir
    
    10 directories, 0 files
    

