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.files(package)¶

Returns an importlib.resources.abc.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.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.

python importlib_importlib --- import 的实现 — Python 3.10.0a2 文档相关推荐

  1. python3.10_概述 — Python 3.10.0a2 文档

    概述¶ "Python 库"中包含了几种不同的组件. 它包含通常被视为语言"核心"中的一部分的数据类型,例如数字和列表.对于这些类型,Python语言核心定义了 ...

  2. python借助jieba包对单独test和txt文档进行中文分词

    python借助jieba包对单独test和txt文档进行中文分词 一.单独test分词 import jieba jieba.cut("大连圣亚在大连") *#输出:<ge ...

  3. Python+Tesseract-OCR识别图片文字并保存到word文档

    目录 使用Python+Tesseract-OCR识别图片文字并保存到word文档 安装Tesseract-OCR 配置Tesseract-OCR 通过CMD验证Tesseract-OCR工作 安装p ...

  4. Python 将excel中的选择题 导入到word文档中

    Python 将excel中的选择题 导入到word文档中 0x00 昨天,我的老师给我们一个包含600道关于比赛的选择题和判断题的excel文档,要我们整理成指定格式的word文档以后交给他.我看着 ...

  5. 自从学会Python后,无视百度文库VIP,所有文档免费下载阅读

    最近要用到百度文库查资料,但是很多都需要付费VIP或者下载券,还不能复制,就有点苦逼! 还好小编会Python,在Python面前真的所有VIP都是小意思,啥视频网站,资料网站等等,统统无视收费机制! ...

  6. HTML5 权威指南第 10 章 文档分节 学习笔记

    HTML5 权威指南第 10 章 文档分节 学习笔记 第 8 章 标记文字 内容从从文字出发,专注如何将单体内容正确的呈现出来:第 9 章 组织内容 内容从段落出发,专注如何将单体内容合理的放在段落中 ...

  7. python爬取文件归类_python爬取各类文档方法归类汇总

    HTML文档是互联网上的主要文档类型,但还存在如TXT.WORD.excel.PDF.csv等多种类型的文档.网络爬虫不仅需要能够抓取HTML中的敏感信息,也需要有抓取其他类型文档的能力.下面简要记录 ...

  8. Python抓取单个网页中所有的PDF文档

    Github博文地址,此处更新可能不是很及时. 1.背景 最近发现算法以及数据结构落下了不少(其实还是大学没怎么好好学,囧rz),考虑到最近的项目结构越来越复杂了,用它来练练思路,就打算复习下数据结构 ...

  9. python基础教程电子版-Python基础教程(第2版)PDF文档下载

    本书是经典教程的全新改版,作者根据Python 3.0版本的种种变化,全面改写了书中内容,做到既能"瞻前"也能"顾后".本书层次鲜明.结构严谨.内容翔实,特别是 ...

最新文章

  1. Java中的引用与C中的指针
  2. java中的“”、“|”、“^”、“~”运算符怎么用?
  3. Flutter 2 源码阅读
  4. matlab求解复杂复数方程,用matlab求解一个两重积分方程(未知数在积分下限,含复数积分)...
  5. 联想微型计算机M3500q怎么拆,联想ThinkCentre 超级Q 23 创新插拔 一机多用
  6. html捉虫游戏,幼儿园体育游戏《小鸡捉虫》教案(精选4篇)
  7. Oracle 与 MySql 区别
  8. Kettle 简介和实例
  9. CentOS6.7 mysql 主从配置
  10. Atitit.软件仪表盘(7)--温度监测子系统--电脑重要部件温度与监控and警报
  11. 基于DS3231实时时钟模块的stm32简易闹钟
  12. 代码分析工具推荐Understand
  13. 让Ubuntu 10.04完美支持Thinkpad小红点Trackpoint
  14. java 基础数据结构_Java实现的基础数据结构
  15. 量化交易入门阶段——净资产收益率的变动值也迷人
  16. angular实现国密算法sm2、sm3和sm4的ts版,基于sm-crypto库实现,前后端实现
  17. 方差分析ANOVA:理论、推导与R语言实现
  18. Python脚本25:将两张图片拼在一起
  19. Android9.0检测摄像头是否存在源码完整案例
  20. 上传App Store的截图尺寸

热门文章

  1. linux uboot启动流程分析,uboot启动流程分析
  2. php维护session,维护带有cookie的PHP session_start()
  3. jexboss工具 -- JBOSS未授权访问漏洞利用
  4. 计算机信息技术为教育带来的变化,信息技术对课堂教学带来的变化
  5. vue 开发过程中遇到的问题
  6. flex 学习篇 ---- 导航类容器
  7. 巧用CSS实现分隔线
  8. 原型 - 实现自己的jQuery
  9. 滚动条造成页面抖动问题
  10. 01 辅助函数之加密函数