@@ -1283,6 +1283,9 @@ Command-line options
12831283Examples
12841284--------
12851285
1286+ Reading examples
1287+ ~~~~~~~~~~~~~~~~~~~
1288+
12861289How to extract an entire tar archive to the current working directory::
12871290
12881291 import tarfile
@@ -1305,21 +1308,6 @@ a generator function instead of a list::
13051308 tar.extractall(members=py_files(tar))
13061309 tar.close()
13071310
1308- How to create an uncompressed tar archive from a list of filenames::
1309-
1310- import tarfile
1311- tar = tarfile.open("sample.tar", "w")
1312- for name in ["foo", "bar", "quux"]:
1313- tar.add(name)
1314- tar.close()
1315-
1316- The same example using the :keyword: `with ` statement::
1317-
1318- import tarfile
1319- with tarfile.open("sample.tar", "w") as tar:
1320- for name in ["foo", "bar", "quux"]:
1321- tar.add(name)
1322-
13231311How to read a gzip compressed tar archive and display some member information::
13241312
13251313 import tarfile
@@ -1334,6 +1322,19 @@ How to read a gzip compressed tar archive and display some member information::
13341322 print("something else.")
13351323 tar.close()
13361324
1325+ Writing examples
1326+ ~~~~~~~~~~~~~~~~
1327+
1328+ How to create and write an archive to stdout using
1329+ :data: `sys.stdout.buffer <sys.stdout> ` in the *fileobj * parameter
1330+ in :meth: `TarFile.add `::
1331+
1332+ import sys
1333+ import tarfile
1334+ with tarfile.open("sample.tar.gz", "w:gz", fileobj=sys.stdout.buffer) as tar:
1335+ for name in ["foo", "bar", "quux"]:
1336+ tar.add(name)
1337+
13371338How to create an archive and reset the user information using the *filter *
13381339parameter in :meth: `TarFile.add `::
13391340
@@ -1346,13 +1347,18 @@ parameter in :meth:`TarFile.add`::
13461347 tar.add("foo", filter=reset)
13471348 tar.close()
13481349
1349- How to create and write an archive to stdout using
1350- :data: `sys.stdout.buffer <sys.stdout> ` in the *fileobj * parameter
1351- in :meth: `TarFile.add `::
1350+ How to create an uncompressed tar archive from a list of filenames::
1351+
1352+ import tarfile
1353+ tar = tarfile.open("sample.tar", "w")
1354+ for name in ["foo", "bar", "quux"]:
1355+ tar.add(name)
1356+ tar.close()
1357+
1358+ The same example using the :keyword: `with ` statement::
13521359
1353- import sys
13541360 import tarfile
1355- with tarfile.open("sample.tar.gz ", "w:gz", fileobj=sys.stdout.buffer ) as tar:
1361+ with tarfile.open("sample.tar", "w" ) as tar:
13561362 for name in ["foo", "bar", "quux"]:
13571363 tar.add(name)
13581364
0 commit comments