[Python] Convert importlib.resources Anchor to Path only when no temporary directory is needed

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    importlib.resources.path allows to convert an Anchor to a Path. Sometimes, this path is just some existing path on the file system (e.g. when a module is loaded from a folder), sometimes, a temporary directory is created (e.g. when a module is loaded from a wheel).

    For detecting file changes in files obtained in this way, I am only interested in the first case, where an existing path is returned. If the files are inside some wheel, it is very unlikely they change during runtime and detection using this way would pose significant computational overhead.

    Is there a way to request a Path only if no temporary directory is required, or somehow detect whether a given path lives inside a temporary directory (in a platform-independent way)?

    MWE for the (undesired) behaviour where it might create a temporary directory:

    import importlib.resources
    import anchor # Some module

    with importlib.resources.path(anchor) as path:
    print(path)

    Continue reading...

Compartilhe esta Página