Changeset 391 for python/trunk/Doc/library/tarfile.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/tarfile.rst
r2 r391 1 .. _tarfile-mod:2 3 1 :mod:`tarfile` --- Read and write tar archive files 4 2 =================================================== … … 13 11 .. sectionauthor:: Lars GustÀbel <lars@gustaebel.de> 14 12 13 **Source code:** :source:`Lib/tarfile.py` 14 15 -------------- 15 16 16 17 The :mod:`tarfile` module makes it possible to read and write tar 17 18 archives, including those using gzip or bz2 compression. 18 (:file:`.zip` files can be read and written using the :mod:`zipfile` module.) 19 Use the :mod:`zipfile` module to read or write :file:`.zip` files, or the 20 higher-level functions in :ref:`shutil <archiving-operations>`. 19 21 20 22 Some facts and figures: … … 142 144 143 145 .. deprecated:: 2.6 144 The :class:`TarFileCompat` class has been deprecated for removal in Python 3.0.146 The :class:`TarFileCompat` class has been removed in Python 3. 145 147 146 148 … … 235 237 object, see :ref:`tarinfo-objects` for details. 236 238 239 A :class:`TarFile` object can be used as a context manager in a :keyword:`with` 240 statement. It will automatically be closed when the block is completed. Please 241 note that in the event of an exception an archive opened for writing will not 242 be finalized; only the internally used file object will be closed. See the 243 :ref:`tar-examples` section for a use case. 244 245 .. versionadded:: 2.7 246 Added support for the context manager protocol. 237 247 238 248 .. 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) … … 390 400 391 401 392 .. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None )402 .. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None) 393 403 394 404 Add the file *name* to the archive. *name* may be any type of file (directory, … … 398 408 it must be a function that takes one filename argument and returns a boolean 399 409 value. Depending on this value the respective file is either excluded 400 (:const:`True`) or added (:const:`False`). 410 (:const:`True`) or added (:const:`False`). If *filter* is specified it must 411 be a function that takes a :class:`TarInfo` object argument and returns the 412 changed :class:`TarInfo` object. If it instead returns :const:`None` the :class:`TarInfo` 413 object will be excluded from the archive. See :ref:`tar-examples` for an 414 example. 401 415 402 416 .. versionchanged:: 2.6 403 417 Added the *exclude* parameter. 418 419 .. versionchanged:: 2.7 420 Added the *filter* parameter. 421 422 .. deprecated:: 2.7 423 The *exclude* parameter is deprecated, please use the *filter* parameter 424 instead. For maximum portability, *filter* should be used as a keyword 425 argument rather than as a positional argument so that code won't be 426 affected when *exclude* is ultimately removed. 404 427 405 428 … … 639 662 tar.add(name) 640 663 tar.close() 664 665 The same example using the :keyword:`with` statement:: 666 667 import tarfile 668 with tarfile.open("sample.tar", "w") as tar: 669 for name in ["foo", "bar", "quux"]: 670 tar.add(name) 641 671 642 672 How to read a gzip compressed tar archive and display some member information:: … … 654 684 tar.close() 655 685 686 How to create an archive and reset the user information using the *filter* 687 parameter in :meth:`TarFile.add`:: 688 689 import tarfile 690 def reset(tarinfo): 691 tarinfo.uid = tarinfo.gid = 0 692 tarinfo.uname = tarinfo.gname = "root" 693 return tarinfo 694 tar = tarfile.open("sample.tar.gz", "w:gz") 695 tar.add("foo", filter=reset) 696 tar.close() 697 656 698 657 699 .. _tar-formats:
Note:
See TracChangeset
for help on using the changeset viewer.