Skip to content

importlib.resources.as_file docs should mention it can provide whole directories #107732

@smheidrich

Description

@smheidrich

Documentation

As pointed out in a thread on the Ideas forum, importlib.resources gained support for extracting/returning whole directories as resources via its as_file() function in Python 3.12.

But the docs make no mention of this and in fact heavily suggest the opposite (emphasis mine):

importlib.resources.as_file(traversable)
Given a Traversable object representing a file, typically from importlib.resources.files(), return a context manager for use in a with statement. [...]

Exiting the context manager cleans up any temporary file created when the resource was extracted from e.g. a zip file.

Use as_file when the Traversable methods (read_text, etc) are insufficient and an actual file on the file system is required.

This should probably be changed to reflect the fact that it can now handle directories as well, not just individual files.

Especially important because being able to extract/return whole directories was a feature that importlib.resources lacked for quite a while compared to its now-deprecated predecessor pkg_resources, so not pointing out that this works now can make people remain hesitant to make the switch.

PR incoming.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixesdocsDocumentation in the Doc dirtopic-importlib

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions