How can I create folder in importing process [python]
Question: How can I create a class that creates directories for both with 1. Initial
values and 2. input arguments ?

This is my code mypath.py

    
    
    import os
    
    myroot = 'D:\\pet'
    
    class path(object)
        dog = os.path.join(myroot, 'dog')
        cat = os.path.join(myroot, 'cat')
    
    # my 1st question (something like this)
        def __init__(self):
            for directory in dir(self):
                if not os.path.exists(directory):
                    os.makedirs(directory)
    
    # my 2nd question (something like this)
        def append(self, newpath)
            for directory in dir(self):
                newdir = os.path.join(directory, newpath)
                if not os.path.exists(newdir):
                    os.makedirs(newdir)
    
            # update attribute in self
                self.directory = newdir
    

When I do

    
    
    import mypath
    

I can see

    
    
    mypath.dog = 'D:\\pet\\dog'
    mypath.cat = 'D:\\pet\\cat'
    

But they are not created, instead I manually have to do like this:

    
    
    os.mkdir(mypath.dog)
    os.mkdir(mypath.cat)
    

So my question #1 is

How can I make the class to automatically create every path whenever I import?

and question #2 is

How can I create a sub function .append() so that

    
    
    mypath.append('ishungry')
    

to automatically update all attributes in path so that

    
    
    mypath.dog = 'D:\\pet\\dog\\ishungry'
    mypath.cat = 'D:\\pet\\cat\\ishungry'
    

Thanks in advance!


Answer: So the logic to make the directories exist in the objects __init__ method;
this doesn't run upon import. That's why when you import the module, the
directories are not made. To make them, you'd have to create an instance of
the 'path' object:

    
    
    import mypath
    
    my_path= mypath.path()
    

Is there a compelling reason to make these directories upon import instead of
upon object initialization? It's generally considered a [code
smell](https://en.wikipedia.org/wiki/Code_smell) to have this type of side
effect upon module import.

A cleaner way to do this might be to have an object that handles all the path
stuff for you, and to work with that object directly:

    
    
    class PathManager(object):
        """Manages file system paths
    
        :param root: The base of all paths
        :type root: String
    
        :param top_dirs: The frist class directories we care about.
        :type top_dirs: List
        """
    
        def __init__(self, root='D:\\pet', top_dirs=None)
            self.root = root
            self.top_dirs = [] if top_dirs is None else top_dirs
            self._config_dir()
    
        def _config_dir(self, new_dir=None)
            """Updates all managed paths to contain new_dir"""
            for folder in self.top_dirs:
                if new_dir is not None:
                    directory = os.path.join(self.root, folder, new_dir)
                else:
                    directory = os.path.join(self.root, folder)
                if not os.path.exists(directory):
                    os.makedirs(directory)
    
        def append(self, new_dir):
            """Add a new subdirectory to all managed paths, then update
               the list of managed paths"""
            # add new paths to file system
            self._config_dir(new_dir=new_dir)
            # update class to know of update managed paths
            self.top_dirs = [os.path.join(self.root, x, new_dir) for x in self.top_dir]
    

Then use it like:

    
    
    from mypath import PathManager
    
    # Creates the initial paths
    path_mgr = PathManager(top_dirs=['dog, 'cat'])
    
    # add a new subdir to managed path
    path_mgr.append('ishungry')
    

