importlib.resources.abc
-- 資源的抽象基底類別¶
原始碼:Lib/importlib/resources/abc.py
在 3.11 版被加入.
- classimportlib.resources.abc.ResourceReader¶
Superseded by TraversableResources
Anabstract base class to provide the ability to readresources.
From the perspective of this ABC, aresource is a binaryartifact that is shipped within a package. Typically this issomething like a data file that lives next to the
__init__.py
file of the package. The purpose of this class is to help abstractout the accessing of such data files so that it does not matter ifthe package and its data file(s) are stored e.g. in a zip fileversus on the file system.For any of methods of this class, aresource argument isexpected to be apath-like object which representsconceptually just a file name. This means that no subdirectorypaths should be included in theresource argument. This isbecause the location of the package the reader is for, acts as the"directory". Hence the metaphor for directories and filenames is packages and resources, respectively. This is also whyinstances of this class are expected to directly correlate toa specific package (instead of potentially representing multiplepackages or a module).
Loaders that wish to support resource reading are expected toprovide a method called
get_resource_reader(fullname)
whichreturns an object implementing this ABC's interface. If the modulespecified by fullname is not a package, this method should returnNone
. An object compatible with this ABC should only bereturned when the specified module is a package.在 3.12 版之後被棄用:Use
importlib.resources.abc.TraversableResources
instead.- abstractmethodopen_resource(resource)¶
Returns an opened,file-like object for binary readingof theresource.
If the resource cannot be found,
FileNotFoundError
israised.
- abstractmethodresource_path(resource)¶
Returns the file system path to theresource.
If the resource does not concretely exist on the file system,raise
FileNotFoundError
.
- abstractmethodis_resource(name)¶
Returns
True
if the namedname is considered a resource.FileNotFoundError
is raised ifname does not exist.
- abstractmethodcontents()¶
Returns aniterable of strings over the contents ofthe package. Do note that it is not required that all namesreturned by the iterator be actual resources, e.g. it isacceptable to return names for which
is_resource()
wouldbe false.Allowing non-resource names to be returned is to allow forsituations where how a package and its resources are storedare known a priori and the non-resource names would be useful.For instance, returning subdirectory names is allowed so thatwhen it is known that the package and resources are stored onthe file system then those subdirectory names can be useddirectly.
The abstract method returns an iterable of no items.
- classimportlib.resources.abc.Traversable¶
An object with a subset of
pathlib.Path
methods suitable fortraversing 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.
- abstractmethoditerdir()¶
Yield Traversable objects in self.
- abstractmethodis_dir()¶
Return
True
if self is a directory.
- abstractmethodis_file()¶
Return
True
if self is a file.
- abstractmethodjoinpath(*pathsegments)¶
Traverse directories according topathsegments and returnthe result as
Traversable
.Eachpathsegments argument may contain multiple names separated byforward 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
implementationsmight not be updated to the latest version of the protocol.For compatibility with such implementations, provide a single argumentwithout path separators to each call tojoinpath
. For example:files.joinpath('subdir').joinpath('subsubdir').joinpath('file.txt')
在 3.11 版的變更:
joinpath
accepts multiplepathsegments, and these segmentsmay contain forward slashes as path separators.Previously, only a singlechild argument was accepted.
- abstractmethod__truediv__(child)¶
Return Traversable child in self.Equivalent to
joinpath(child)
.
- abstractmethodopen(mode='r',*args,**kwargs)¶
mode may be 'r' or 'rb' to open as text or binary. Return a handlesuitable for reading (same as
pathlib.Path.open
).When opening as text, accepts encoding parameters such as thoseaccepted by
io.TextIOWrapper
.
- read_bytes()¶
Read contents of self as bytes.
- read_text(encoding=None)¶
Read contents of self as text.
- classimportlib.resources.abc.TraversableResources¶
An abstract base class for resource readers capable of servingthe
importlib.resources.files()
interface. SubclassesResourceReader
and providesconcrete implementations of theResourceReader
'sabstract methods. Therefore, any loader supplyingTraversableResources
also suppliesResourceReader
.Loaders that wish to support resource reading are expected toimplement this interface.
- abstractmethodfiles()¶
Returns a
importlib.resources.abc.Traversable
object for the loadedpackage.