aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/utils/filesystem.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/mlia/utils/filesystem.py')
-rw-r--r--src/mlia/utils/filesystem.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/mlia/utils/filesystem.py b/src/mlia/utils/filesystem.py
index 73a88d9..7975905 100644
--- a/src/mlia/utils/filesystem.py
+++ b/src/mlia/utils/filesystem.py
@@ -122,3 +122,21 @@ def copy_all(*paths: Path, dest: Path) -> None:
if path.is_dir():
shutil.copytree(path, dest, dirs_exist_ok=True)
+
+
+@contextmanager
+def working_directory(
+ working_dir: Path, create_dir: bool = False
+) -> Generator[Path, None, None]:
+ """Temporary change working directory."""
+ current_working_dir = Path.cwd()
+
+ if create_dir:
+ working_dir.mkdir()
+
+ os.chdir(working_dir)
+
+ try:
+ yield working_dir
+ finally:
+ os.chdir(current_working_dir)