Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/tarfile.rst

    r2 r391  
    1 .. _tarfile-mod:
    2 
    31:mod:`tarfile` --- Read and write tar archive files
    42===================================================
     
    1311.. sectionauthor:: Lars GustÀbel <lars@gustaebel.de>
    1412
     13**Source code:** :source:`Lib/tarfile.py`
     14
     15--------------
    1516
    1617The :mod:`tarfile` module makes it possible to read and write tar
    1718archives, including those using gzip or bz2 compression.
    18 (:file:`.zip` files can be read and written using the :mod:`zipfile` module.)
     19Use the :mod:`zipfile` module to read or write :file:`.zip` files, or the
     20higher-level functions in :ref:`shutil <archiving-operations>`.
    1921
    2022Some facts and figures:
     
    142144
    143145   .. 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.
    145147
    146148
     
    235237object, see :ref:`tarinfo-objects` for details.
    236238
     239A :class:`TarFile` object can be used as a context manager in a :keyword:`with`
     240statement. It will automatically be closed when the block is completed. Please
     241note that in the event of an exception an archive opened for writing will not
     242be 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.
    237247
    238248.. 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)
     
    390400
    391401
    392 .. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None)
     402.. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)
    393403
    394404   Add the file *name* to the archive. *name* may be any type of file (directory,
     
    398408   it must be a function that takes one filename argument and returns a boolean
    399409   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.
    401415
    402416   .. versionchanged:: 2.6
    403417      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.
    404427
    405428
     
    639662       tar.add(name)
    640663   tar.close()
     664
     665The 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)
    641671
    642672How to read a gzip compressed tar archive and display some member information::
     
    654684   tar.close()
    655685
     686How to create an archive and reset the user information using the *filter*
     687parameter 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
    656698
    657699.. _tar-formats:
Note: See TracChangeset for help on using the changeset viewer.