pyiron_snippets.directory_context module

pyiron_snippets.directory_context.set_directory(path: Path | str)[source]

A context manager that changes the current working directory to the given path upon entering the context and reverts to the original directory upon exiting. If the specified path does not exist, it is created.

Parameters:

path – Path | str The target directory path to set as the current working directory. If it does not exist, it will be created.

Examples

Change the directory to the path “tmp” within the context:

>>> import os
>>> from pyiron_snippets.directory_context import set_directory
>>> directory_before_context_is_applied = os.getcwd()
>>> with set_directory("tmp"):
...     os.path.relpath(os.getcwd(), directory_before_context_is_applied)
'tmp'