上一话题

importlib.resources – Package resource reading, opening and access

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 导入模块
  9. importlib.resources.abc – 用于资源的抽象基类

importlib.resources.abc – 用于资源的抽象基类 ¶

源代码: Lib/importlib/resources/abc.py


Added in version 3.11.

class importlib.resources.abc. ResourceReader ¶

Superseded by TraversableResources

An 抽象基类 to provide the ability to read resources .

From the perspective of this ABC, a resource is a binary artifact that is shipped within a package. Typically this is something like a data file that lives next to the __init__.py file of the package. The purpose of this class is to help abstract out the accessing of such data files so that it does not matter if the package and its data file(s) are stored in a e.g. zip file versus on the file system.

For any of methods of this class, a resource argument is expected to be a 像路径对象 which represents conceptually just a file name. This means that no subdirectory paths should be included in the resource argument. This is because the location of the package the reader is for, acts as the “directory”. Hence the metaphor for directories and file names is packages and resources, respectively. This is also why instances of this class are expected to directly correlate to a specific package (instead of potentially representing multiple packages or a module).

Loaders that wish to support resource reading are expected to provide a method called get_resource_reader(fullname) which returns an object implementing this ABC’s interface. If the module specified by fullname is not a package, this method should return None . An object compatible with this ABC should only be returned when the specified module is a package.

Deprecated since version 3.12, will be removed in version 3.14: 使用 importlib.resources.abc.TraversableResources 代替。

abstractmethod open_resource ( resource ) ¶

Returns an opened, 像文件对象 for binary reading of the resource .

If the resource cannot be found, FileNotFoundError 被引发。

abstractmethod resource_path ( resource ) ¶

Returns the file system path to the resource .

If the resource does not concretely exist on the file system, raise FileNotFoundError .

abstractmethod is_resource ( name ) ¶

返回 True if the named name is considered a resource. FileNotFoundError 被引发若 name does not exist.

abstractmethod contents ( ) ¶

返回 iterable of strings over the contents of the package. Do note that it is not required that all names returned by the iterator be actual resources, e.g. it is acceptable to return names for which is_resource() would be false.

Allowing non-resource names to be returned is to allow for situations where how a package and its resources are stored are known a priori and the non-resource names would be useful. For instance, returning subdirectory names is allowed so that when it is known that the package and resources are stored on the file system then those subdirectory names can be used directly.

The abstract method returns an iterable of no items.

class importlib.resources.abc. Traversable ¶

An object with a subset of pathlib.Path methods suitable for traversing directories and opening files.

For a representation of the object on the file-system, use importlib.resources.as_file() .

name ¶

Abstract. The base name of this object without any parent references.

abstractmethod iterdir ( ) ¶

Yield Traversable objects in self.

abstractmethod is_dir ( ) ¶

返回 True if self is a directory.

abstractmethod is_file ( ) ¶

返回 True if self is a file.

abstractmethod joinpath ( * pathsegments ) ¶

Traverse directories according to pathsegments and return the result as Traversable .

每个 pathsegments argument may contain multiple names separated by forward slashes ( / , posixpath.sep ). For example, the following are equivalent:

files.joinpath('subdir', 'subsuddir', 'file.txt')
files.joinpath('subdir/subsuddir/file.txt')
														

Note that some Traversable implementations might not be updated to the latest version of the protocol. For compatibility with such implementations, provide a single argument without path separators to each call to joinpath 。例如:

files.joinpath('subdir').joinpath('subsubdir').joinpath('file.txt')
														

3.11 版改变: joinpath accepts multiple pathsegments , and these segments may contain forward slashes as path separators. Previously, only a single child argument was accepted.

abstractmethod __truediv__ ( child ) ¶

Return Traversable child in self. Equivalent to joinpath(child) .

abstractmethod open ( mode = 'r' , * args , ** kwargs ) ¶

mode may be ‘r’ or ‘rb’ to open as text or binary. Return a handle suitable for reading (same as pathlib.Path.open ).

When opening as text, accepts encoding parameters such as those accepted by io.TextIOWrapper .

read_bytes ( ) ¶

Read contents of self as bytes.

read_text ( encoding = None ) ¶

Read contents of self as text.

class importlib.resources.abc. TraversableResources ¶

An abstract base class for resource readers capable of serving the importlib.resources.files() interface. Subclasses ResourceReader and provides concrete implementations of the ResourceReader ’s abstract methods. Therefore, any loader supplying TraversableResources also supplies ResourceReader .

Loaders that wish to support resource reading are expected to implement this interface.

abstractmethod 文件 ( ) ¶

返回 importlib.resources.abc.Traversable object for the loaded package.

上一话题

importlib.resources – Package resource reading, opening and access

下一话题

importlib.metadata – Accessing package metadata

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 导入模块
  9. importlib.resources.abc – 用于资源的抽象基类

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1