Skip to content

Commit 42b622e

Browse files
committed
gh-136858: Adds an example of writing a tarfile using stdin
1 parent ee72c95 commit 42b622e

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

Doc/library/tarfile.rst

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Some facts and figures:
104104
| ``'w'`` or | Open for uncompressed writing. |
105105
| ``'w:'`` | |
106106
+------------------+---------------------------------------------+
107-
| ``'w:gz'`` | Open for gzip compressed writing. |
107+
| ``'w:gz'`` | Open for gzip-compressed writing. |
108108
+------------------+---------------------------------------------+
109109
| ``'w:bz2'`` | Open for bzip2 compressed writing. |
110110
+------------------+---------------------------------------------+
@@ -159,7 +159,7 @@ Some facts and figures:
159159
| ``'r|'`` | Open a *stream* of uncompressed tar blocks |
160160
| | for reading. |
161161
+-------------+--------------------------------------------+
162-
| ``'r|gz'`` | Open a gzip compressed *stream* for |
162+
| ``'r|gz'`` | Open a gzip-compressed *stream* for |
163163
| | reading. |
164164
+-------------+--------------------------------------------+
165165
| ``'r|bz2'`` | Open a bzip2 compressed *stream* for |
@@ -173,7 +173,7 @@ Some facts and figures:
173173
+-------------+--------------------------------------------+
174174
| ``'w|'`` | Open an uncompressed *stream* for writing. |
175175
+-------------+--------------------------------------------+
176-
| ``'w|gz'`` | Open a gzip compressed *stream* for |
176+
| ``'w|gz'`` | Open a gzip-compressed *stream* for |
177177
| | writing. |
178178
+-------------+--------------------------------------------+
179179
| ``'w|bz2'`` | Open a bzip2 compressed *stream* for |
@@ -1367,6 +1367,12 @@ How to extract an entire tar archive to the current working directory::
13671367
tar.extractall(filter='data')
13681368
tar.close()
13691369

1370+
The same example using the :keyword:`with` statement::
1371+
1372+
import tarfile
1373+
with tarfile.open("sample.tar.gz") as tar:
1374+
tar.extractall(filter='data')
1375+
13701376
How to extract a subset of a tar archive with :meth:`TarFile.extractall` using
13711377
a generator function instead of a list::
13721378

@@ -1382,7 +1388,7 @@ a generator function instead of a list::
13821388
tar.extractall(members=py_files(tar))
13831389
tar.close()
13841390

1385-
How to read a gzip compressed tar archive and display some member information::
1391+
How to read a gzip-compressed tar archive and display some member information::
13861392

13871393
import tarfile
13881394
tar = tarfile.open("sample.tar.gz", "r:gz")
@@ -1396,6 +1402,15 @@ How to read a gzip compressed tar archive and display some member information::
13961402
print("something else.")
13971403
tar.close()
13981404

1405+
How to extract a gzip-compressed tar archive from standard input using
1406+
:data:`sys.stdin.buffer <sys.stdin>` as the *fileobj* parameter
1407+
in :meth:`TarFile.open` into the current working directory::
1408+
1409+
import sys
1410+
import tarfile
1411+
with tarfile.open(fileobj=sys.stdin.buffer, mode="r:gz") as tar:
1412+
tar.extractall(filter="data")
1413+
13991414
Writing examples
14001415
~~~~~~~~~~~~~~~~
14011416

@@ -1414,9 +1429,18 @@ The same example using the :keyword:`with` statement::
14141429
for name in ["foo", "bar", "quux"]:
14151430
tar.add(name)
14161431

1432+
How to create a gzip-compressed tar archive from a list of filenames
1433+
read from the standard input using :data:`sys.stdin <sys.stdin>`::
1434+
1435+
import sys
1436+
import tarfile
1437+
with tarfile.open("sample.tar.gz", mode="w|gz") as tar:
1438+
for filename in sys.stdin:
1439+
tar.add(filename.strip())
1440+
14171441
How to create and write an archive to stdout using
14181442
:data:`sys.stdout.buffer <sys.stdout>` in the *fileobj* parameter
1419-
in :meth:`TarFile.add`::
1443+
in :meth:`TarFile.open`::
14201444

14211445
import sys
14221446
import tarfile

0 commit comments

Comments
 (0)