quemb.shared.manage_scratch.WorkDir¶
- class quemb.shared.manage_scratch.WorkDir(path, cleanup_at_end=True)¶
Manage a scratch area.
Upon initialisation of the object the workdir
path
is created, if it does not exist yet. One can either enter apath
themselves, or if it isNone
, then the path is automatically determined by the environment, i.e. are we on SLURM, where is the scratch etc. Read more atfrom_environment()
for more details.Note that the
/
is overloaded for this class and it can be used aspathlib.Path
in that regard, see example below.If
cleanup_at_end
is true, thencleanup()
method is registered to be called when the program terminates with no errors and deletes the scratch directory. TheWorkDir
also exists as a ContextManager; then the cleanup is performed when leaving the ContextManager. Again, assuming thatcleanup_at_end
is true.Examples
>>> with WorkDir('./test_dir', cleanup_at_end=True) as scratch: >>> with open(scratch / 'test.txt', 'w') as f: >>> f.write('hello world') './test_dir' does not exist anymore, if the outer contextmanager is left without errors.
Attributes
Methods
__init__
(path[, cleanup_at_end])Method generated by attrs for class WorkDir.
cleanup
([ignore_error])Conditionally cleanup the working directory.
from_environment
(*[, user_defined_root, ...])Create a WorkDir based on the environment.