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/shutil.rst

    r2 r391  
    1 
    21:mod:`shutil` --- High-level file operations
    32============================================
     
    1211   single: copying files
    1312
     13**Source code:** :source:`Lib/shutil.py`
     14
     15--------------
     16
    1417The :mod:`shutil` module offers a number of high-level operations on files and
    1518collections of files.  In particular, functions are provided  which support file
     
    1922.. warning::
    2023
    21    Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
    22    can't copy all file metadata.
     24   Even the higher-level file copying functions (:func:`shutil.copy`,
     25   :func:`shutil.copy2`) can't copy all file metadata.
    2326
    2427   On POSIX platforms, this means that file owner and group are lost as well
     
    2831   are not copied.
    2932
     33
     34.. _file-operations:
     35
     36Directory and files operations
     37------------------------------
    3038
    3139.. function:: copyfileobj(fsrc, fdst[, length])
     
    4250.. function:: copyfile(src, dst)
    4351
    44    Copy the contents (no metadata) of the file named *src* to a file named *dst*.
    45    *dst* must be the complete target file name; look at :func:`copy` for a copy that
    46    accepts a target directory path.  If *src* and *dst* are the same files,
    47    :exc:`Error` is raised.
     52   Copy the contents (no metadata) of the file named *src* to a file named
     53   *dst*.  *dst* must be the complete target file name; look at
     54   :func:`shutil.copy` for a copy that accepts a target directory path.  If
     55   *src* and *dst* are the same files, :exc:`Error` is raised.
    4856   The destination location must be writable; otherwise,  an :exc:`IOError` exception
    4957   will be raised. If *dst* already exists, it will be replaced.   Special files
     
    7583.. function:: copy2(src, dst)
    7684
    77    Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just
    78    :func:`copy` followed by :func:`copystat`.  This is similar to the
    79    Unix command :program:`cp -p`.
     85   Similar to :func:`shutil.copy`, but metadata is copied as well -- in fact,
     86   this is just :func:`shutil.copy` followed by :func:`copystat`.  This is
     87   similar to the Unix command :program:`cp -p`.
    8088
    8189
     
    8997
    9098
    91 .. function:: copytree(src, dst[, symlinks=False[, ignore=None]])
     99.. function:: copytree(src, dst, symlinks=False, ignore=None)
    92100
    93101   Recursively copy an entire directory tree rooted at *src*.  The destination
    94    directory, named by *dst*, must not already exist; it will be created as well
    95    as missing parent directories.  Permissions and times of directories are
    96    copied with :func:`copystat`, individual files are copied using
    97    :func:`copy2`.
     102   directory, named by *dst*, must not already exist; it will be created as
     103   well as missing parent directories.  Permissions and times of directories
     104   are copied with :func:`copystat`, individual files are copied using
     105   :func:`shutil.copy2`.
    98106
    99107   If *symlinks* is true, symbolic links in the source tree are represented as
    100    symbolic links in the new tree; if false or omitted, the contents of the
    101    linked files are copied to the new tree.
     108   symbolic links in the new tree, but the metadata of the original links is NOT
     109   copied; if false or omitted, the contents and metadata of the linked files
     110   are copied to the new tree.
    102111
    103112   If *ignore* is given, it must be a callable that will receive as its
     
    154163.. function:: move(src, dst)
    155164
    156    Recursively move a file or directory to another location.
    157 
    158    If the destination is on the current filesystem, then simply use rename.
    159    Otherwise, copy src (with :func:`copy2`) to the dst and then remove src.
     165   Recursively move a file or directory (*src*) to another location (*dst*).
     166
     167   If the destination is a directory or a symlink to a directory, then *src* is
     168   moved inside that directory.
     169
     170   The destination directory must not already exist.  If the destination already
     171   exists but is not a directory, it may be overwritten depending on
     172   :func:`os.rename` semantics.
     173
     174   If the destination is on the current filesystem, then :func:`os.rename` is
     175   used.  Otherwise, *src* is copied (using :func:`shutil.copy2`) to *dst* and
     176   then removed.
    160177
    161178   .. versionadded:: 2.3
     
    164181.. exception:: Error
    165182
    166    This exception collects exceptions that raised during a multi-file operation. For
    167    :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
    168    *dstname*, *exception*).
     183   This exception collects exceptions that are raised during a multi-file
     184   operation. For :func:`copytree`, the exception argument is a list of 3-tuples
     185   (*srcname*, *dstname*, *exception*).
    169186
    170187   .. versionadded:: 2.3
    171188
    172189
    173 .. _shutil-example:
    174 
    175 Example
    176 -------
     190.. _copytree-example:
     191
     192copytree example
     193::::::::::::::::
    177194
    178195This example is the implementation of the :func:`copytree` function, described
     
    203220                   copy2(srcname, dstname)
    204221               # XXX What about devices, sockets etc.?
    205            except (IOError, os.error), why:
     222           except (IOError, os.error) as why:
    206223               errors.append((srcname, dstname, str(why)))
    207224           # catch the Error from the recursive copytree so that we can
    208225           # continue with other files
    209            except Error, err:
     226           except Error as err:
    210227               errors.extend(err.args[0])
    211228       try:
     
    214231           # can't copy file access times on Windows
    215232           pass
    216        except OSError, why:
     233       except OSError as why:
    217234           errors.extend((src, dst, str(why)))
    218235       if errors:
     
    239256   copytree(source, destination, ignore=_logpath)
    240257
     258
     259.. _archiving-operations:
     260
     261Archiving operations
     262--------------------
     263
     264High-level utilities to create and read compressed and archived files are also
     265provided.  They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
     266
     267.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
     268
     269   Create an archive file (eg. zip or tar) and returns its name.
     270
     271   *base_name* is the name of the file to create, including the path, minus
     272   any format-specific extension. *format* is the archive format: one of
     273   "zip", "tar", "bztar" or "gztar".
     274
     275   *root_dir* is a directory that will be the root directory of the
     276   archive; ie. we typically chdir into *root_dir* before creating the
     277   archive.
     278
     279   *base_dir* is the directory where we start archiving from;
     280   ie. *base_dir* will be the common prefix of all files and
     281   directories in the archive.
     282
     283   *root_dir* and *base_dir* both default to the current directory.
     284
     285   *owner* and *group* are used when creating a tar archive. By default,
     286   uses the current owner and group.
     287
     288   *logger* must be an object compatible with :pep:`282`, usually an instance of
     289   :class:`logging.Logger`.
     290
     291   .. versionadded:: 2.7
     292
     293
     294.. function:: get_archive_formats()
     295
     296   Return a list of supported formats for archiving.
     297   Each element of the returned sequence is a tuple ``(name, description)``
     298
     299   By default :mod:`shutil` provides these formats:
     300
     301   - *gztar*: gzip'ed tar-file
     302   - *bztar*: bzip2'ed tar-file
     303   - *tar*: uncompressed tar file
     304   - *zip*: ZIP file
     305
     306   You can register new formats or provide your own archiver for any existing
     307   formats, by using :func:`register_archive_format`.
     308
     309   .. versionadded:: 2.7
     310
     311
     312.. function:: register_archive_format(name, function, [extra_args, [description]])
     313
     314   Register an archiver for the format *name*. *function* is a callable that
     315   will be used to invoke the archiver.
     316
     317   If given, *extra_args* is a sequence of ``(name, value)`` that will be
     318   used as extra keywords arguments when the archiver callable is used.
     319
     320   *description* is used by :func:`get_archive_formats` which returns the
     321   list of archivers. Defaults to an empty list.
     322
     323   .. versionadded:: 2.7
     324
     325
     326.. function::  unregister_archive_format(name)
     327
     328   Remove the archive format *name* from the list of supported formats.
     329
     330   .. versionadded:: 2.7
     331
     332
     333.. _archiving-example:
     334
     335Archiving example
     336:::::::::::::::::
     337
     338In this example, we create a gzip'ed tar-file archive containing all files
     339found in the :file:`.ssh` directory of the user::
     340
     341    >>> from shutil import make_archive
     342    >>> import os
     343    >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
     344    >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
     345    >>> make_archive(archive_name, 'gztar', root_dir)
     346    '/Users/tarek/myarchive.tar.gz'
     347
     348The resulting archive contains::
     349
     350    $ tar -tzvf /Users/tarek/myarchive.tar.gz
     351    drwx------ tarek/staff       0 2010-02-01 16:23:40 ./
     352    -rw-r--r-- tarek/staff     609 2008-06-09 13:26:54 ./authorized_keys
     353    -rwxr-xr-x tarek/staff      65 2008-06-09 13:26:54 ./config
     354    -rwx------ tarek/staff     668 2008-06-09 13:26:54 ./id_dsa
     355    -rwxr-xr-x tarek/staff     609 2008-06-09 13:26:54 ./id_dsa.pub
     356    -rw------- tarek/staff    1675 2008-06-09 13:26:54 ./id_rsa
     357    -rw-r--r-- tarek/staff     397 2008-06-09 13:26:54 ./id_rsa.pub
     358    -rw-r--r-- tarek/staff   37192 2010-02-06 18:23:10 ./known_hosts
Note: See TracChangeset for help on using the changeset viewer.