otwrapy.TempWorkDir

class TempWorkDir(base_temp_work_dir=None, prefix='run-', cleanup=False, transfer=None)

Implement a context manager that creates a temporary working directory.

Create a temporary working directory on base_temp_work_dir preceded by prefix and clean up at the exit if necessary. See: http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/

Parameters:
base_temp_work_dirstr (optional)

Root path where the temporary working directory will be created. If None, it will default to the platform dependant temporary working directory Default = None

prefixstr (optional)

String that preceeds the directory name. Default = ‘run-’

cleanupbool (optional)

If True erase the directory and its children at the exit. Default = False

transferlist (optional)

List of files or folders to transfer to the temporary working directory

Examples

In the following example, everything that is executed inside the with environment will happen at a temporary working directory created at /tmp with /run- as a prefix. The created directory will be erased upon the exit of the with environment and python will go back to the preceding working directory, even if an Exception is raised.

>>> import otwrapy as otw
>>> import os
>>> print "I'm on my project directory"
>>> print os.getcwd()
>>> with otw.TempWorkDir('/tmp', prefix='run-', cleanup=True):
>>>     #
>>>     # Do stuff
>>>     #
>>>     print "..."
>>>     print "Now I'm in a temporary directory"
>>>     print os.getcwd()
>>>     print "..."
>>> print "I'm back to my project directory :"
>>> print os.getcwd()
I'm on my project directory
/home/aguirre/otwrapy
...
Now I'm in a temporary directory
/tmp/run-pZYpzQ
...
I'm back to my project directory :
/home/aguirre/otwrapy
__init__(base_temp_work_dir=None, prefix='run-', cleanup=False, transfer=None)

Methods

__init__([base_temp_work_dir, prefix, ...])