From 0d2cc2b08f3e2cc344725233091e631b66237b72 Mon Sep 17 00:00:00 2001 From: dhanusharer Date: Wed, 25 Mar 2026 20:04:11 +0530 Subject: [PATCH] gh-146396: Add extractfile() example to tarfile Reading Examples and clarify TarInfo.size --- Doc/library/tarfile.rst | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst index a86469bb9ad704..47c504813284ba 100644 --- a/Doc/library/tarfile.rst +++ b/Doc/library/tarfile.rst @@ -810,7 +810,7 @@ A ``TarInfo`` object has the following public data attributes: .. attribute:: TarInfo.size :type: int - Size in bytes. + Size of the archived file's data in bytes. .. attribute:: TarInfo.mtime @@ -1387,6 +1387,17 @@ a generator function instead of a list:: tar.extractall(members=py_files(tar)) tar.close() +How to read the content of a specific archive member into memory:: + + import tarfile + + with tarfile.open("sample.tar.gz") as tar: + for member in tar: + f = tar.extractfile(member) + if f is not None: + content = f.read() + break + How to read a gzip compressed tar archive and display some member information:: import tarfile