python import 路径_importlib

3.7 新版功能.

这个模块使得Python的导入系统提供了访问*包*内的*资源*的功能。如果能够导入一个包,那么就能够访问那个包里面的资源。资源可以以二进制或文本模式方式被打开或读取。

资源非常类似于目录内部的文件,要牢记的是这仅仅是一个比喻。资源和包不是与文件系统上的物理文件和目录一样存在着。

注解

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.

加载器想要支持资源读取应该实现一个由 importlib.abc.ResourceReader 指定的``get_resource_reader(fullname)`` 方法。

The following types are defined.

importlib.resources.Package¶

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

that where the function describes accepting a Package, you can pass in

either a string or a module. Module objects must have a resolvable

__spec__.submodule_search_locations that is not None.

importlib.resources.Resource¶

This type describes the resource names passed into the various functions

in this package. This is defined as Union[str, os.PathLike].

The following functions are available.

importlib.resources.open_binary(package, resource)¶

Open for binary reading the resource within package.

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.

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.

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.

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.

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).

importlib.resources.is_resource(package, name)¶

Return True if there is a resource named name in the package,

otherwise False. Remember that directories are not resources!

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

Package requirements.

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 is either a name or a module object which conforms to the

Package requirements.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值