importlib.resources – Resources

Source code: Lib/importlib/resources/__init__.py


3.7 新版功能.

这个模块利用 Python 的导入系统来提供对包内资源的访问。如果您可以导入一个包,那么您就可以访问该包中的资源。资源可以以二进制或文本模式打开或读取。

Resources are roughly akin to files inside directories, though it’s important to keep in mind that this is just a metaphor. Resources and packages do not have to exist as physical files and directories on the file system: for example, a package and its resources can be imported from a zip file using zipimport.

备注

This module provides functionality similar to pkg_resources Basic Resource Access without the performance overhead of that package. This makes reading resources included in packages easier, with more stable and consistent semantics.

The standalone backport of this module provides more information on using importlib.resources and migrating from pkg_resources to importlib.resources.

Loaders that wish to support resource reading should implement a get_resource_reader(fullname) method as specified by importlib.resources.abc.ResourceReader.

importlib.resources.Package

Whenever a function accepts a Package argument, you can pass in either a module object or a module name as a string. You can only pass module objects whose __spec__.submodule_search_locations is not None.

The Package type is defined as Union[str, ModuleType].

importlib.resources.files(package)

Returns a Traversable object representing the resource container for the package (think directory) and its resources (think files). A Traversable may contain other containers (think subdirectories).

package is either a name or a module object which conforms to the Package requirements.

3.9 新版功能.

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. The context manager provides a pathlib.Path object.

退出上下文管理器会清除资源提取时创建的任何临时文件,例如 zip 文件。

当 Traversable 方法(read_text 等)不足且需要文件系统上的实际文件时,使用 as_file

3.9 新版功能.

Deprecated functions

An older, deprecated set of functions is still available, but is scheduled for removal in a future version of Python. The main drawback of these functions is that they do not support directories: they assume all resources are located directly within a package.

importlib.resources.Resource

For resource arguments of the functions below, you can pass in the name of a resource as a string or a path-like object.

The Resource type is defined as Union[str, os.PathLike].

importlib.resources.open_binary(package, resource)

打开用于二进制读取 package 中的 resource

package is either a name or a module object which conforms to the Package requirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). This function returns a typing.BinaryIO instance, a binary I/O stream open for reading.

3.11 版后已移除: Calls to this function can be replaced by:

files(package).joinpath(resource).open('rb')
importlib.resources.open_text(package, resource, encoding='utf-8', errors='strict')

Open for text reading the resource within package. By default, the resource is opened for reading as UTF-8.

package is either a name or a module object which conforms to the Package requirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). encoding and errors have the same meaning as with built-in open().

This function returns a typing.TextIO instance, a text I/O stream open for reading.

3.11 版后已移除: Calls to this function can be replaced by:

files(package).joinpath(resource).open('r', encoding=encoding)
importlib.resources.read_binary(package, resource)

Read and return the contents of the resource within package as bytes.

package is either a name or a module object which conforms to the Package requirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). This function returns the contents of the resource as bytes.

3.11 版后已移除: Calls to this function can be replaced by:

files(package).joinpath(resource).read_bytes()
importlib.resources.read_text(package, resource, encoding='utf-8', errors='strict')

Read and return the contents of resource within package as a str. By default, the contents are read as strict UTF-8.

package is either a name or a module object which conforms to the Package requirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory). encoding and errors have the same meaning as with built-in open(). This function returns the contents of the resource as str.

3.11 版后已移除: Calls to this function can be replaced by:

files(package).joinpath(resource).read_text(encoding=encoding)
importlib.resources.path(package, resource)

Return the path to the resource as an actual file system path. This function returns a context manager for use in a with statement. The context manager provides a pathlib.Path object.

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

package is either a name or a module object which conforms to the Package requirements. resource is the name of the resource to open within package; it may not contain path separators and it may not have sub-resources (i.e. it cannot be a directory).

3.11 版后已移除: Calls to this function can be replaced using as_file():

as_file(files(package).joinpath(resource))
importlib.resources.is_resource(package, name)

Return True if there is a resource named name in the package, otherwise False. This function does not consider directories to be resources. package is either a name or a module object which conforms to the Package requirements.

3.11 版后已移除: Calls to this function can be replaced by:

files(package).joinpath(resource).is_file()
importlib.resources.contents(package)

Return an iterable over the named items within the package. The iterable returns str resources (e.g. files) and non-resources (e.g. directories). The iterable does not recurse into subdirectories.

package 可以是一个符合 Package 要求的名称,也可以是一个模块对象。

3.11 版后已移除: Calls to this function can be replaced by:

(resource.name for resource in files(package).iterdir() if resource.is_file())