Skip to content

Commit 0d2cc2b

Browse files
committed
gh-146396: Add extractfile() example to tarfile Reading Examples and clarify TarInfo.size
1 parent 0055140 commit 0d2cc2b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Doc/library/tarfile.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ A ``TarInfo`` object has the following public data attributes:
810810
.. attribute:: TarInfo.size
811811
:type: int
812812

813-
Size in bytes.
813+
Size of the archived file's data in bytes.
814814

815815

816816
.. attribute:: TarInfo.mtime
@@ -1387,6 +1387,17 @@ a generator function instead of a list::
13871387
tar.extractall(members=py_files(tar))
13881388
tar.close()
13891389

1390+
How to read the content of a specific archive member into memory::
1391+
1392+
import tarfile
1393+
1394+
with tarfile.open("sample.tar.gz") as tar:
1395+
for member in tar:
1396+
f = tar.extractfile(member)
1397+
if f is not None:
1398+
content = f.read()
1399+
break
1400+
13901401
How to read a gzip compressed tar archive and display some member information::
13911402

13921403
import tarfile

0 commit comments

Comments
 (0)