cpython/Doc/lib/libshutil.tex
Guido van Rossum 486364b821 Merged revisions 56020-56124 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk

................
  r56037 | georg.brandl | 2007-06-19 05:33:20 -0700 (Tue, 19 Jun 2007) | 2 lines

  Patch #1739659: don't slice dict.keys() in pydoc.
................
  r56060 | martin.v.loewis | 2007-06-21 13:00:02 -0700 (Thu, 21 Jun 2007) | 2 lines

  Regenerate to add True, False, None.
................
  r56069 | neal.norwitz | 2007-06-21 22:31:56 -0700 (Thu, 21 Jun 2007) | 1 line

  Get the doctest working again after adding None, True, and False as kewyords.
................
  r56070 | neal.norwitz | 2007-06-21 23:25:33 -0700 (Thu, 21 Jun 2007) | 1 line

  Add space to error message.
................
  r56071 | neal.norwitz | 2007-06-21 23:40:04 -0700 (Thu, 21 Jun 2007) | 6 lines

  Get pybench working, primarily
   * Use print function
   * Stop using string module
   * Use sorted instead of assuming dict methods return lists
   * Convert range result to a list
................
  r56089 | collin.winter | 2007-06-26 10:31:48 -0700 (Tue, 26 Jun 2007) | 1 line

  Fix AttributeError in distutils/dir_util.py.
................
  r56124 | guido.van.rossum | 2007-06-29 18:04:31 -0700 (Fri, 29 Jun 2007) | 30 lines

  Merged revisions 56014-56123 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r56019 | lars.gustaebel | 2007-06-18 04:42:11 -0700 (Mon, 18 Jun 2007) | 2 lines

    Added exclude keyword argument to the TarFile.add() method.
  ........
    r56023 | lars.gustaebel | 2007-06-18 13:05:55 -0700 (Mon, 18 Jun 2007) | 3 lines

    Added missing \versionchanged tag for the new exclude
    parameter.
  ........
    r56038 | georg.brandl | 2007-06-19 05:36:00 -0700 (Tue, 19 Jun 2007) | 2 lines

    Bug #1737864: allow empty message in logging format routines.
  ........
    r56040 | georg.brandl | 2007-06-19 05:38:20 -0700 (Tue, 19 Jun 2007) | 2 lines

    Bug #1739115: make shutil.rmtree docs clear wrt. file deletion.
  ........
    r56084 | georg.brandl | 2007-06-25 08:21:23 -0700 (Mon, 25 Jun 2007) | 2 lines

    Bug #1742901: document None behavior of shlex.split.
  ........
    r56091 | georg.brandl | 2007-06-27 07:09:56 -0700 (Wed, 27 Jun 2007) | 2 lines

    Fix a variable name in winreg docs.
  ........
................
2007-06-30 05:01:58 +00:00

152 lines
6.1 KiB
TeX

\section{\module{shutil} ---
High-level file operations}
\declaremodule{standard}{shutil}
\modulesynopsis{High-level file operations, including copying.}
\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
% partly based on the docstrings
The \module{shutil} module offers a number of high-level operations on
files and collections of files. In particular, functions are provided
which support file copying and removal.
\index{file!copying}
\index{copying files}
\strong{Caveat:} On MacOS, the resource fork and other metadata are
not used. For file copies, this means that resources will be lost and
file type and creator codes will not be correct.
\begin{funcdesc}{copyfile}{src, dst}
Copy the contents of the file named \var{src} to a file named
\var{dst}. The destination location must be writable; otherwise,
an \exception{IOError} exception will be raised.
If \var{dst} already exists, it will be replaced.
Special files such as character or block devices
and pipes cannot be copied with this function. \var{src} and
\var{dst} are path names given as strings.
\end{funcdesc}
\begin{funcdesc}{copyfileobj}{fsrc, fdst\optional{, length}}
Copy the contents of the file-like object \var{fsrc} to the
file-like object \var{fdst}. The integer \var{length}, if given,
is the buffer size. In particular, a negative \var{length} value
means to copy the data without looping over the source data in
chunks; by default the data is read in chunks to avoid uncontrolled
memory consumption. Note that if the current file position of the
\var{fsrc} object is not 0, only the contents from the current file
position to the end of the file will be copied.
\end{funcdesc}
\begin{funcdesc}{copymode}{src, dst}
Copy the permission bits from \var{src} to \var{dst}. The file
contents, owner, and group are unaffected. \var{src} and \var{dst}
are path names given as strings.
\end{funcdesc}
\begin{funcdesc}{copystat}{src, dst}
Copy the permission bits, last access time, last modification time,
and flags from \var{src} to \var{dst}. The file contents, owner, and
group are unaffected. \var{src} and \var{dst} are path names given
as strings.
\end{funcdesc}
\begin{funcdesc}{copy}{src, dst}
Copy the file \var{src} to the file or directory \var{dst}. If
\var{dst} is a directory, a file with the same basename as \var{src}
is created (or overwritten) in the directory specified. Permission
bits are copied. \var{src} and \var{dst} are path names given as
strings.
\end{funcdesc}
\begin{funcdesc}{copy2}{src, dst}
Similar to \function{copy()}, but last access time and last
modification time are copied as well. This is similar to the
\UNIX{} command \program{cp} \programopt{-p}.
\end{funcdesc}
\begin{funcdesc}{copytree}{src, dst\optional{, symlinks}}
Recursively copy an entire directory tree rooted at \var{src}. The
destination directory, named by \var{dst}, must not already exist;
it will be created as well as missing parent directories.
Permissions and times of directories are copied with \function{copystat()},
individual files are copied using \function{copy2()}.
If \var{symlinks} is true, symbolic links in
the source tree are represented as symbolic links in the new tree;
if false or omitted, the contents of the linked files are copied to
the new tree. If exception(s) occur, an \exception{Error} is raised
with a list of reasons.
The source code for this should be considered an example rather than
a tool.
\versionchanged[\exception{Error} is raised if any exceptions occur during
copying, rather than printing a message]{2.3}
\versionchanged[Create intermediate directories needed to create \var{dst},
rather than raising an error. Copy permissions and times of
directories using \function{copystat()}]{2.5}
\end{funcdesc}
\begin{funcdesc}{rmtree}{path\optional{, ignore_errors\optional{, onerror}}}
\index{directory!deleting}
Delete an entire directory tree (\var{path} must point to a directory).
If \var{ignore_errors} is true, errors resulting from failed removals
will be ignored; if false or omitted, such errors are handled by
calling a handler specified by \var{onerror} or, if that is omitted,
they raise an exception.
If \var{onerror} is provided, it must be a callable that accepts
three parameters: \var{function}, \var{path}, and \var{excinfo}.
The first parameter, \var{function}, is the function which raised
the exception; it will be \function{os.listdir()}, \function{os.remove()} or
\function{os.rmdir()}. The second parameter, \var{path}, will be
the path name passed to \var{function}. The third parameter,
\var{excinfo}, will be the exception information return by
\function{sys.exc_info()}. Exceptions raised by \var{onerror} will
not be caught.
\end{funcdesc}
\begin{funcdesc}{move}{src, dst}
Recursively move a file or directory to another location.
If the destination is on our current filesystem, then simply use
rename. Otherwise, copy src to the dst and then remove src.
\versionadded{2.3}
\end{funcdesc}
\begin{excdesc}{Error}
This exception collects exceptions that raised during a mult-file
operation. For \function{copytree}, the exception argument is a
list of 3-tuples (\var{srcname}, \var{dstname}, \var{exception}).
\versionadded{2.3}
\end{excdesc}
\subsection{Example \label{shutil-example}}
This example is the implementation of the \function{copytree()}
function, described above, with the docstring omitted. It
demonstrates many of the other functions provided by this module.
\begin{verbatim}
def copytree(src, dst, symlinks=0):
names = os.listdir(src)
os.mkdir(dst)
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
copy2(srcname, dstname)
except (IOError, os.error) as why:
print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
\end{verbatim}