< All Topics

Storing a sequence of deposition runs in separate directories

If you’re running a series of deposition runs, it might be necessary to preserve the output of each. Since a deposition run will overwrite a previous run, a new directory would need to be made for each deposition run. This article will show how this can be done. For the purpose of this, we’ll omit the deposition run, and instead just create some directories.

from muk3d.util import set_working_directory
import os

current_run = 1

for i in range(5):
    dir_name = 'run-{}'.format(current_run)
    os.mkdir(dir_name)
    set_working_directory(dir_name)
    
    # do stuff
    
    set_working_directory('..')
    current_run += 1

In line 1, the function set_working_directory is imported from the muk3d.util module.  This will set the working directory similar to how you would manually do it by right clicking in the Project Manager. The os module is imported since that has a command we can use to create a new directory.

Since we need each directory to have a unique name, a counter variable called current_run is created and assigned the value of 1.  

Next a for loop will be run 5 times. Each time, a new directory will be created using the name ‘run-<current_run>’ and so the format method is used on the string to place the current_run value into the string.

In line 8 the function mkdir from the os module (called using os.mkdir since we only imported the module, not the mkdir function, which we could have done instead). This will then make a new directory in the working directory.  To set this to be our working directory, use the set_working_directory command with the name of the newly created directory as the argument,

Now, the new directory is our working directory and we can do whatever else needs to be done in the script.  Once completed, we need to go back to the original working directory and this can be done by calling set_working_directory with ‘..’ as the argument.  ‘..’ is the syntax used to go up to the parent directory.  This will make the working directory to be whatever it was when the script was started.  This way, when we make the next directory for the next run, it will sit beside the first directory, and not be nested within it.

The final step in line 14 is to increment the current_run variable by 1.

Run the script and you should see 5 new directories created in the working directory.

Note that is the script has an error midway through and the working directory has been changed, you’ll need to manually reset the working directory in the Project Manager.  If you don’t then the next level of directories will be created in whatever directory you’re currently in.

Dealing with errors

Now run the script again.  You should see that there’s a WindowsError and it can’t create the new directory because it already exists. This exception is raised by the os.mkdir command which will fail if the directory name already exists.  To get around this, there would be two options.  The first and simplest is to ignore the error completely.  We can do that using a try/except statement around the os.mkdir call.

from muk3d.util import set_working_directory
import os

current_run = 1

for i in range(5):
    dir_name = 'run-{}'.format(current_run)
    
    try:
        os.mkdir(dir_name)
    except WindowsError as e:
        pass 
        
    set_working_directory(dir_name)
    
    # do stuff
    
    set_working_directory('..')
    current_run += 1

In rows 9-12 the os.mkdir will execute within the try statement.  If there’s an error (i.e. the directory exists, the code in the except block will be executed.  In this case, the pass keyword means nothing will happen and the error will pass silently, allowing the script to continue on.

Verifying that the working directory is being set

 If we want to verify that each directory is indeed being set as the working directory, we can get the script to print out the current directory as its looping through.  The current directory name can be retrieved using the os.getcwd function.

from muk3d.util import set_working_directory
import os

current_run = 1

for i in range(5):
    dir_name = 'run-{}'.format(current_run)
    
    try:
        os.mkdir(dir_name)
    except WindowsError as e:
        pass 
        
    set_working_directory(dir_name)
    
    # do stuff
    print os.getcwd()
    
    set_working_directory('..')
    current_run += 1

Now when the script is run, each directory name will be printed to the output window.

Referencing files in other directories

Something to be careful of is how to reference files in earlier directories (e.g. Run 2 might build off the output_grid.mgrid in Run 1). For the example below, the base grid is assumed to exist in the working directory where the script is being executed from and is defined in line 5.

from muk3d.util import set_working_directory
import os

current_run = 1
base_grid = 'test.grid' # exits in working directory

for i in range(5):
    dir_name = 'run-{}'.format(current_run)
    
    try:
        os.mkdir(dir_name)
    except WindowsError as e:
        pass 
            
    set_working_directory(dir_name)
    base_for_this_run = os.path.join('..', base_grid)
    
    # do stuff
    print os.getcwd()
    
    base_grid = os.path.join(dir_name, 'output_grid.mgrid')
    
    set_working_directory('..')
    
    current_run += 1

Once we’ve created the new directory and set it as the working and tried to use the base_grid variable, we’d likely have an error, since the working directory wouldn’t have that particular file in it.  Instead, its sitting up in the parent directory.  The proper path to the file can be created using the os.path.join function.  This will take file/directory names and stitch them together into a valid path.  

In line 16, ‘..’ (go up to the parent directory) and the base_grid name are turned into a variable that will correctly reference the base grid.

Once the first run is complete, the base for the next run is going to be the output_grid.mgrid from this run, and so the base_grid variable is redefined to reference this file using the directory name.  In line 21 the directory name and file name are turned into a path that is references this grid.

Now, when the second and subsequent loops are run, the base file will be set to be previous runs output grid.

Table of Contents