Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7626ef9

Browse files
committed
Add an entry for tarfile.
1 parent a199368 commit 7626ef9

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Doc/whatsnew/3.2.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,38 @@ wrong results.
13181318

13191319
(Patch submitted by Nir Aides in :issue:`7610`.)
13201320

1321+
tarfile
1322+
-------
1323+
1324+
The :class:`~tarfile.TarFile` class can now be used as a content manager. In
1325+
addition, its :meth:`~tarfile.TarFile.add` method has a new option, *filter*,
1326+
that controls which files are added to the archive and allows the file metadata
1327+
to be edited.
1328+
1329+
The new *filter* option replaces the older, less flexible *exclude* parameter
1330+
which is now deprecated. If specified, the optional *filter* parameter needs to
1331+
be a :term:`keyword argument`. The user-supplied filter function accepts a
1332+
:class:`~tarfile.TarInfo` object and returns an updated
1333+
:class:`~tarfile.TarInfo` object, or if it wants the file to be excluded, the
1334+
function can return *None*::
1335+
1336+
>>> import tarfile, glob
1337+
1338+
>>> def myfilter(tarinfo):
1339+
if tarinfo.isfile(): # only save real files
1340+
tarinfo.uname = 'monty' # redact the user name
1341+
return tarinfo
1342+
1343+
>>> with tarfile.TarFile(name='myarchive.tar', mode='w') as tf:
1344+
for filename in glob.glob('*.txt'):
1345+
tf.add(filename, filter=myfilter)
1346+
tf.list()
1347+
-rw-r--r-- monty/501 902 2011-01-26 17:59:11 annotations.txt
1348+
-rw-r--r-- monty/501 123 2011-01-26 17:59:11 general_questions.txt
1349+
-rw-r--r-- monty/501 3514 2011-01-26 17:59:11 prion.txt
1350+
-rw-r--r-- monty/501 124 2011-01-26 17:59:11 py_todo.txt
1351+
-rw-r--r-- monty/501 1399 2011-01-26 17:59:11 semaphore_notes.txt
1352+
13211353
hashlib
13221354
-------
13231355

0 commit comments

Comments
 (0)