rxn.utilities.files.named_temporary_path

rxn.utilities.files.named_temporary_path(delete=True)[source]

Get the path for a temporary file or directory, without creating it (can be especially useful in tests).

This is similar to tempfile.NamedTemporaryFile, when the file is not to be actually opened, and one is just interested in obtaining a writable / readable path to optionally delete at the end of the context.

This function was originally created to bypass a limitation of NamedTemporaryFile on Windows (https://stackoverflow.com/q/23212435), which becomes relevant when one does not want the file to be opened automatically. The solution is inspired by https://stackoverflow.com/a/58955530.

Parameters

delete (bool, default: True) – whether to delete the file when exiting the context

Return type

Iterator[Path]

Examples

>>> with named_temporary_path() as temporary_path:
...     # do something on the temporary path.
...     # The file or directory at that path will be deleted at the
...     # end of the context, except if delete=False.
Return type

Iterator[Path]

Parameters

delete (bool) –