blob: deb2ed2797121b8c2ddcd419d858fd0b0322656b [file] [log] [blame]
Fred Drake449e18f1998-12-28 20:16:58 +00001\section{\module{shutil} ---
Fred Drakedbd72a41999-02-01 21:27:59 +00002 High-level file operations}
Fred Drake449e18f1998-12-28 20:16:58 +00003
4\declaremodule{standard}{shutil}
Fred Drakedbd72a41999-02-01 21:27:59 +00005\modulesynopsis{High-level file operations, including copying.}
Fred Drake449e18f1998-12-28 20:16:58 +00006\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
7% partly based on the docstrings
8
9
10The \module{shutil} module offers a number of high-level operations on
11files and collections of files. In particular, functions are provided
12which support file copying and removal.
Fred Drake94c4a791998-12-28 21:58:57 +000013\index{file!copying}
14\index{copying files}
Fred Drake449e18f1998-12-28 20:16:58 +000015
16\strong{Caveat:} On MacOS, the resource fork and other metadata are
17not used. For file copies, this means that resources will be lost and
18file type and creator codes will not be correct.
19
20
21\begin{funcdesc}{copyfile}{src, dst}
Fred Drake043d5e52001-03-02 16:46:42 +000022 Copy the contents of the file named \var{src} to a file named
23 \var{dst}. If \var{dst} exists, it will be replaced, otherwise it
Fred Drake757f7802001-09-04 18:26:27 +000024 will be created. Special files such as character or block devices
25 and pipes cannot not be copied with this function. \var{src} and
26 \var{dst} are path names given as strings.
Fred Drake449e18f1998-12-28 20:16:58 +000027\end{funcdesc}
28
Fred Drake578a3f92000-07-31 15:45:46 +000029\begin{funcdesc}{copyfileobj}{fsrc, fdst\optional{, length}}
30 Copy the contents of the file-like object \var{fsrc} to the
31 file-like object \var{fdst}. The integer \var{length}, if given,
32 is the buffer size. In particular, a negative \var{length} value
33 means to copy the data without looping over the source data in
34 chunks; by default the data is read in chunks to avoid uncontrolled
35 memory consumption.
36\end{funcdesc}
37
Fred Drake449e18f1998-12-28 20:16:58 +000038\begin{funcdesc}{copymode}{src, dst}
39 Copy the permission bits from \var{src} to \var{dst}. The file
Fred Drake757f7802001-09-04 18:26:27 +000040 contents, owner, and group are unaffected. \var{src} and \var{dst}
41 are path names given as strings.
Fred Drake449e18f1998-12-28 20:16:58 +000042\end{funcdesc}
43
44\begin{funcdesc}{copystat}{src, dst}
45 Copy the permission bits, last access time, and last modification
46 time from \var{src} to \var{dst}. The file contents, owner, and
Fred Drake757f7802001-09-04 18:26:27 +000047 group are unaffected. \var{src} and \var{dst} are path names given
48 as strings.
Fred Drake449e18f1998-12-28 20:16:58 +000049\end{funcdesc}
50
51\begin{funcdesc}{copy}{src, dst}
52 Copy the file \var{src} to the file or directory \var{dst}. If
53 \var{dst} is a directory, a file with the same basename as \var{src}
54 is created (or overwritten) in the directory specified. Permission
Fred Drake757f7802001-09-04 18:26:27 +000055 bits are copied. \var{src} and \var{dst} are path names given as
56 strings.
Fred Drake449e18f1998-12-28 20:16:58 +000057\end{funcdesc}
58
59\begin{funcdesc}{copy2}{src, dst}
60 Similar to \function{copy()}, but last access time and last
61 modification time are copied as well. This is similar to the
Fred Draked290c101999-11-09 18:03:00 +000062 \UNIX{} command \program{cp} \programopt{-p}.
Fred Drake449e18f1998-12-28 20:16:58 +000063\end{funcdesc}
64
65\begin{funcdesc}{copytree}{src, dst\optional{, symlinks}}
66 Recursively copy an entire directory tree rooted at \var{src}. The
67 destination directory, named by \var{dst}, must not already exist;
68 it will be created. Individual files are copied using
69 \function{copy2()}. If \var{symlinks} is true, symbolic links in
70 the source tree are represented as symbolic links in the new tree;
71 if false or omitted, the contents of the linked files are copied to
72 the new tree. Errors are reported to standard output.
73
74 The source code for this should be considered an example rather than
75 a tool.
76\end{funcdesc}
77
78\begin{funcdesc}{rmtree}{path\optional{, ignore_errors\optional{, onerror}}}
Fred Drake94c4a791998-12-28 21:58:57 +000079\index{directory!deleting}
Fred Drake449e18f1998-12-28 20:16:58 +000080 Delete an entire directory tree. If \var{ignore_errors} is true,
81 errors will be ignored; if false or omitted, errors are handled by
82 calling a handler specified by \var{onerror} or raise an exception.
83
84 If \var{onerror} is provided, it must be a callable that accepts
85 three parameters: \var{function}, \var{path}, and \var{excinfo}.
86 The first parameter, \var{function}, is the function which raised
87 the exception; it will be \function{os.remove()} or
88 \function{os.rmdir()}. The second parameter, \var{path}, will be
89 the path name passed to \var{function}. The third parameter,
90 \var{excinfo}, will be the exception information return by
91 \function{sys.exc_info()}. Exceptions raised by \var{onerror} will
92 not be caught.
93\end{funcdesc}
94
95
96\subsection{Example \label{shutil-example}}
97
98This example is the implementation of the \function{copytree()}
Fred Drake11bc8cf1999-04-21 17:08:51 +000099function, described above, with the docstring omitted. It
100demonstrates many of the other functions provided by this module.
Fred Drake449e18f1998-12-28 20:16:58 +0000101
102\begin{verbatim}
103def copytree(src, dst, symlinks=0):
104 names = os.listdir(src)
105 os.mkdir(dst)
106 for name in names:
107 srcname = os.path.join(src, name)
108 dstname = os.path.join(dst, name)
109 try:
110 if symlinks and os.path.islink(srcname):
111 linkto = os.readlink(srcname)
112 os.symlink(linkto, dstname)
113 elif os.path.isdir(srcname):
114 copytree(srcname, dstname)
115 else:
116 copy2(srcname, dstname)
117 # XXX What about devices, sockets etc.?
118 except (IOError, os.error), why:
119 print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
120\end{verbatim}