]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43644: Add docs for importlib.resources.as_file. (GH-25048)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 28 Mar 2021 00:48:53 +0000 (17:48 -0700)
committerGitHub <noreply@github.com>
Sun, 28 Mar 2021 00:48:53 +0000 (17:48 -0700)
(cherry picked from commit af50c84643ce21cfbdfdabbdfae6bd5e1368c542)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Doc/library/importlib.rst

index cb2a5686a95a50023a4ccee8d570a26ab516694a..61a81e02ff88fc344f04c435b9a6257e28f97f36 100644 (file)
@@ -888,6 +888,22 @@ The following functions are available.
 
     .. versionadded:: 3.9
 
+.. function:: as_file(traversable)
+
+    Given a :class:`importlib.resources.abc.Traversable` object representing
+    a file, typically from :func:`importlib.resources.files`, return
+    a context manager for use in a :keyword:`with` statement.
+    The context manager provides a :class:`pathlib.Path` object.
+
+    Exiting the context manager cleans up any temporary file created when the
+    resource was extracted from e.g. a zip file.
+
+    Use ``as_file`` when the Traversable methods
+    (``read_text``, etc) are insufficient and an actual file on
+    the file system is required.
+
+    .. versionadded:: 3.9
+
 .. function:: open_binary(package, resource)
 
     Open for binary reading the *resource* within *package*.