2

I have a nested dictionary like this:

Main = {
    'root' : {
        'object': 'data'
        'users': {
            'otherobj': 'data'
        }
    }
}
  • I need to create a file tree from it in the local dir /tree/ where the name of each dict is the name of the folder
  • All values within each dict belong to it
  • All values that are not dict's are ignored. (I'll handle those separately)
  • The depth of the tree will be unknown.
  • Each dir can(and usually will) have multiple sub dirs.
2
  • @ZdaR A hand full of google false-magic that didn't help me at all. Truth is i don't know what to try. That's why i'm here. Commented Feb 18, 2017 at 18:49
  • 1
    Useful functions for manipulating files and directories are in the os, os.path and shutil modules. You could use them from a recursive function that walks through your dict of dicts. Commented Feb 18, 2017 at 18:54

2 Answers 2

1

This code snippet should help. Here I assume that the python script will be run in target local directory.

import os

# dir_definition = <your 'Main'>

def create_dirs(dir_definition):
    for dir_dame, sub_dir in dir_definition.items():
        os.mkdir(dir_dame)
        if isinstance(sub_dir, dict):
            create_dirs(sub_dir)
Sign up to request clarification or add additional context in comments.

1 Comment

The problem is indeed begging for a recursive solution.
0

If I'm understanding properly, you want to end up with something like the following:

  • root/object containing data
  • root/users/otherobj containing data
  • and so on

So let's break this down a bit. You basically need the ability to do two things:

  • determine whether the value of a key in your dictionary is "data" or another dictionary
  • given some data, write it out to a file with a given path

I'm going to leave it to you to write these functions, but I'll assume they have the following signatures:

  • def is_data(obj) returning True/False (this could be not isinstanceof(obj, dict) unless your data objects could be dicts with some special properties)
  • def write_data(directory, filename, obj)

At this point, we're ready to write a function to walk the tree. I'm going to assume you pull the first key and dictionary out of Main. For each (key, value) pair, we need to check if the value is "data" or if it's another dict. If it's data, write it out. If it's another dict, we add the key to our path and call our function on that dict.

def walk(root_directory, obj_dict):
    for k, v in obj_dict.iteritems():
        if is_data(v):
            # write the file
            write_data(root_directory, k, v)
        else: # it's another dict, so recurse
            # add the key to the path
            new_root = os.path.join(root_directory, k) # you'll need to import os
            walk(new_root, v)

5 Comments

If the objects in the dict are not dict's (eg: 'something': 'that isn't a dict') it needs to be ignored.
Actually I can just impliment that here. But will this create the directories if no files exist within the dict's?
(by that, i mean ignore my first comment)
I need to create the entire file-tree regardless of if the files exist or not. And each dir can(and usually will) have multiple sub dirs.
With a minor edit, this solved my problem. I just added if not os.path.exists(new_root): os.makedirs(new_root) right under new_root = os.path.join(root_directory, k)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.