diff options
author | Raymond Hettinger <python@rcn.com> | 2011-01-27 05:48:56 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-01-27 05:48:56 (GMT) |
commit | 7626ef93b72891d6fb244c86a52fa9eafc239ff3 (patch) | |
tree | d0290cb014af547af8fdde2c66428f798f313b4f /Doc | |
parent | a199368b230026fa37987852fe8802992a126ab7 (diff) | |
download | cpython-7626ef93b72891d6fb244c86a52fa9eafc239ff3.zip cpython-7626ef93b72891d6fb244c86a52fa9eafc239ff3.tar.gz cpython-7626ef93b72891d6fb244c86a52fa9eafc239ff3.tar.bz2 |
Add an entry for tarfile.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/whatsnew/3.2.rst | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.2.rst b/Doc/whatsnew/3.2.rst index 50f84ec..a1bdb97 100644 --- a/Doc/whatsnew/3.2.rst +++ b/Doc/whatsnew/3.2.rst @@ -1318,6 +1318,38 @@ wrong results. (Patch submitted by Nir Aides in :issue:`7610`.) +tarfile +------- + +The :class:`~tarfile.TarFile` class can now be used as a content manager. In +addition, its :meth:`~tarfile.TarFile.add` method has a new option, *filter*, +that controls which files are added to the archive and allows the file metadata +to be edited. + +The new *filter* option replaces the older, less flexible *exclude* parameter +which is now deprecated. If specified, the optional *filter* parameter needs to +be a :term:`keyword argument`. The user-supplied filter function accepts a +:class:`~tarfile.TarInfo` object and returns an updated +:class:`~tarfile.TarInfo` object, or if it wants the file to be excluded, the +function can return *None*:: + + >>> import tarfile, glob + + >>> def myfilter(tarinfo): + if tarinfo.isfile(): # only save real files + tarinfo.uname = 'monty' # redact the user name + return tarinfo + + >>> with tarfile.TarFile(name='myarchive.tar', mode='w') as tf: + for filename in glob.glob('*.txt'): + tf.add(filename, filter=myfilter) + tf.list() + -rw-r--r-- monty/501 902 2011-01-26 17:59:11 annotations.txt + -rw-r--r-- monty/501 123 2011-01-26 17:59:11 general_questions.txt + -rw-r--r-- monty/501 3514 2011-01-26 17:59:11 prion.txt + -rw-r--r-- monty/501 124 2011-01-26 17:59:11 py_todo.txt + -rw-r--r-- monty/501 1399 2011-01-26 17:59:11 semaphore_notes.txt + hashlib ------- |