mirror of
https://github.com/python/cpython.git
synced 2025-11-18 01:57:37 +00:00
Merged revisions 78623 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r78623 | lars.gustaebel | 2010-03-03 12:55:48 +0100 (Wed, 03 Mar 2010) | 3 lines Issue #7232: Add support for the context manager protocol to the TarFile class. ........
This commit is contained in:
parent
5749e85b53
commit
0138581c43
4 changed files with 92 additions and 0 deletions
|
|
@ -209,6 +209,14 @@ a header block followed by data blocks. It is possible to store a file in a tar
|
|||
archive several times. Each archive member is represented by a :class:`TarInfo`
|
||||
object, see :ref:`tarinfo-objects` for details.
|
||||
|
||||
A :class:`TarFile` object can be used as a context manager in a :keyword:`with`
|
||||
statement. It will automatically be closed when the block is completed. Please
|
||||
note that in the event of an exception an archive opened for writing will not
|
||||
be finalized, only the internally used file object will be closed. See the
|
||||
:ref:`tar-examples` section for a use case.
|
||||
|
||||
.. versionadded:: 3.2
|
||||
Added support for the context manager protocol.
|
||||
|
||||
.. class:: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0)
|
||||
|
||||
|
|
@ -593,6 +601,13 @@ How to create an uncompressed tar archive from a list of filenames::
|
|||
tar.add(name)
|
||||
tar.close()
|
||||
|
||||
The same example using the :keyword:`with` statement::
|
||||
|
||||
import tarfile
|
||||
with tarfile.open("sample.tar", "w") as tar:
|
||||
for name in ["foo", "bar", "quux"]:
|
||||
tar.add(name)
|
||||
|
||||
How to read a gzip compressed tar archive and display some member information::
|
||||
|
||||
import tarfile
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue