@@ -97,3 +97,181 @@ for example, a package and its resources can be imported from a zip file using
97
97
98
98
.. versionchanged :: 3.12
99
99
Added support for *traversable * representing a directory.
100
+
101
+
102
+ .. _importlib_resources_functional :
103
+
104
+ Functional API
105
+ ^^^^^^^^^^^^^^
106
+
107
+ A set of simplified, backwards-compatible helpers is available.
108
+ These allow common operations in a single function call.
109
+
110
+ For all the following functions:
111
+
112
+ - *anchor * is an :class: `~importlib.resources.Anchor `,
113
+ as in :func: `~importlib.resources.files `.
114
+ Unlike in ``files ``, it may not be omitted.
115
+
116
+ - *path_names * are components of a resource's path name, relative to
117
+ the anchor.
118
+ For example, to get the text of resource named ``info.txt ``, use::
119
+
120
+ importlib.resources.read_text(my_module, "info.txt")
121
+
122
+ Like :meth: `Traversable.joinpath <importlib.resources.abc.Traversable> `,
123
+ The individual components should use forward slashes (``/ ``)
124
+ as path separators.
125
+ For example, the following are equivalent::
126
+
127
+ importlib.resources.read_binary(my_module, "pics/painting.png")
128
+ importlib.resources.read_binary(my_module, "pics", "painting.png")
129
+
130
+ For backward compatibility reasons, functions that read text require
131
+ an explicit *encoding * argument if multiple *path_names * are given.
132
+ For example, to get the text of ``info/chapter1.txt ``, use::
133
+
134
+ importlib.resources.read_text(my_module, "info", "chapter1.txt",
135
+ encoding='utf-8')
136
+
137
+ .. function :: open_binary(anchor, *path_names)
138
+
139
+ Open the named resource for binary reading.
140
+
141
+ See :ref: `the introduction <importlib_resources_functional >` for
142
+ details on *anchor * and *path_names *.
143
+
144
+ This function returns a :class: `~typing.BinaryIO ` object,
145
+ that is, a binary stream open for reading.
146
+
147
+ This function is roughly equivalent to::
148
+
149
+ files(anchor).joinpath(*path_names).open('rb')
150
+
151
+ .. versionchanged :: 3.13
152
+ Multiple *path_names * are accepted.
153
+
154
+
155
+ .. function :: open_text(anchor, *path_names, encoding='utf-8', errors='strict')
156
+
157
+ Open the named resource for text reading.
158
+ By default, the contents are read as strict UTF-8.
159
+
160
+ See :ref: `the introduction <importlib_resources_functional >` for
161
+ details on *anchor * and *path_names *.
162
+ *encoding * and *errors * have the same meaning as in built-in :func: `open `.
163
+
164
+ For backward compatibility reasons, the *encoding * argument must be given
165
+ explicitly if there are multiple *path_names *.
166
+ This limitation is scheduled to be removed in Python 3.15.
167
+
168
+ This function returns a :class: `~typing.TextIO ` object,
169
+ that is, a text stream open for reading.
170
+
171
+ This function is roughly equivalent to::
172
+
173
+ files(anchor).joinpath(*path_names).open('r', encoding=encoding)
174
+
175
+ .. versionchanged :: 3.13
176
+ Multiple *path_names * are accepted.
177
+ *encoding * and *errors * must be given as keyword arguments.
178
+
179
+
180
+ .. function :: read_binary(anchor, *path_names)
181
+
182
+ Read and return the contents of the named resource as :class: `bytes `.
183
+
184
+ See :ref: `the introduction <importlib_resources_functional >` for
185
+ details on *anchor * and *path_names *.
186
+
187
+ This function is roughly equivalent to::
188
+
189
+ files(anchor).joinpath(*path_names).read_bytes()
190
+
191
+ .. versionchanged :: 3.13
192
+ Multiple *path_names * are accepted.
193
+
194
+
195
+ .. function :: read_text(anchor, *path_names, encoding='utf-8', errors='strict')
196
+
197
+ Read and return the contents of the named resource as :class: `str `.
198
+ By default, the contents are read as strict UTF-8.
199
+
200
+ See :ref: `the introduction <importlib_resources_functional >` for
201
+ details on *anchor * and *path_names *.
202
+ *encoding * and *errors * have the same meaning as in built-in :func: `open `.
203
+
204
+ For backward compatibility reasons, the *encoding * argument must be given
205
+ explicitly if there are multiple *path_names *.
206
+ This limitation is scheduled to be removed in Python 3.15.
207
+
208
+ This function is roughly equivalent to::
209
+
210
+ files(anchor).joinpath(*path_names).read_text(encoding=encoding)
211
+
212
+ .. versionchanged :: 3.13
213
+ Multiple *path_names * are accepted.
214
+ *encoding * and *errors * must be given as keyword arguments.
215
+
216
+
217
+ .. function :: path(anchor, *path_names)
218
+
219
+ Provides the path to the *resource * as an actual file system path. This
220
+ function returns a context manager for use in a :keyword: `with ` statement.
221
+ The context manager provides a :class: `pathlib.Path ` object.
222
+
223
+ Exiting the context manager cleans up any temporary files created, e.g.
224
+ when the resource needs to be extracted from a zip file.
225
+
226
+ For example, the :meth: `~pathlib.Path.stat ` method requires
227
+ an actual file system path; it can be used like this::
228
+
229
+ with importlib.resources.path(anchor, "resource.txt") as fspath:
230
+ result = fspath.stat()
231
+
232
+ See :ref: `the introduction <importlib_resources_functional >` for
233
+ details on *anchor * and *path_names *.
234
+
235
+ This function is roughly equivalent to::
236
+
237
+ as_file(files(anchor).joinpath(*path_names))
238
+
239
+ .. versionchanged :: 3.13
240
+ Multiple *path_names * are accepted.
241
+ *encoding * and *errors * must be given as keyword arguments.
242
+
243
+
244
+ .. function :: is_resource(anchor, *path_names)
245
+
246
+ Return ``True `` if the named resource exists, otherwise ``False ``.
247
+ This function does not consider directories to be resources.
248
+
249
+ See :ref: `the introduction <importlib_resources_functional >` for
250
+ details on *anchor * and *path_names *.
251
+
252
+ This function is roughly equivalent to::
253
+
254
+ files(anchor).joinpath(*path_names).is_file()
255
+
256
+ .. versionchanged :: 3.13
257
+ Multiple *path_names * are accepted.
258
+
259
+
260
+ .. function :: contents(anchor, *path_names)
261
+
262
+ Return an iterable over the named items within the package or path.
263
+ The iterable returns names of resources (e.g. files) and non-resources
264
+ (e.g. directories) as :class: `str `.
265
+ The iterable does not recurse into subdirectories.
266
+
267
+ See :ref: `the introduction <importlib_resources_functional >` for
268
+ details on *anchor * and *path_names *.
269
+
270
+ This function is roughly equivalent to::
271
+
272
+ for resource in files(anchor).joinpath(*path_names).iterdir():
273
+ yield resource.name
274
+
275
+ .. deprecated :: 3.11
276
+ Prefer ``iterdir() `` as above, which offers more control over the
277
+ results and richer functionality.
0 commit comments