work_directory¶
- work_directory(workdir: str, create: bool = False, cleanup: bool = False, reset: bool = True, capture_log: bool | str = False, subdir: bool = False, reraise_on_error: bool = True) Generator[str, None, None][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 -- The path to the target directory.
create -- If True, creates the directory (and subdirectories if subdir is True) if it does not already exist.
cleanup -- If True, recursively removes all files and directories within the target working directory before execution.
reset -- If True, resets CASA log files and other modules before execution and upon exit.
capture_log -- 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 -- 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 -- 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.
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