work_directory¶
- work_directory(workdir, create=False, cleanup=False, reset=True, capture_log=False, subdir=False, reraise_on_error=True)[source]¶
A context manager to temporarily change the working directory.
Changes the current working directory for a Pipeline session to the specified path. Optionally, it can restore the original directory upon exiting the context, create the directory, clean its contents, and manage CASA-specific configurations (e.g. casalog files)
- Parameters:
workdir (str) -- The path to the target directory.
create (bool) -- If True, creates the directory (and subdirectories if subdir is True) if it does not already exist.
cleanup (bool) -- If True, recursively removes all files and directories within the target working directory before execution.
reset (bool) -- If True, resets CASA log files and other modules before execution and upon exit.
capture_log (bool | str) -- If True, captures CASA logs to a new timestamped log file. If a string is provided, it is used as the log file path.
subdir (bool) -- If True, creates and uses a standard subdirectory structure (products, working, rawdata) within workdir. The context will change into the 'working' subdirectory.
reraise_on_error (bool) -- If True, exceptions will be re-raised instead of just being logged.
- Yields:
The absolute path to the CASA log file being used within the context.
- Return type:
Generator[str, None, None]
Example
- with work_directory('/tmp/my_analysis', create=True, cleanup=True) as log_file:
print(f"Working in directory. Logs are being saved to {log_file}") # Your code runs here, in the '/tmp/my_analysis' directory