blob: 9f79c0fe74596e13c7dcf80b43a658dafd6ab489 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`shutil` --- High-level file operations
3============================================
4
5.. module:: shutil
6 :synopsis: High-level file operations, including copying.
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Georg Brandlb19be572007-12-29 10:57:00 +00008.. partly based on the docstrings
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
10.. index::
11 single: file; copying
12 single: copying files
13
14The :mod:`shutil` module offers a number of high-level operations on files and
15collections of files. In particular, functions are provided which support file
Mark Summerfieldac3d4292007-11-02 08:24:59 +000016copying and removal. For operations on individual files, see also the
17:mod:`os` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000018
Georg Brandlbf863b12007-08-15 19:06:04 +000019.. warning::
Georg Brandlec32b6b2008-01-06 16:12:39 +000020
21 Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
22 can't copy all file metadata.
Georg Brandlbf863b12007-08-15 19:06:04 +000023
Georg Brandlec32b6b2008-01-06 16:12:39 +000024 On POSIX platforms, this means that file owner and group are lost as well
25 as ACLs. On MacOS, the resource fork and other metadata are not used.
26 This means that resources will be lost and file type and creator codes will
27 not be correct. On Windows, file owners, ACLs and alternate data streams
28 are not copied.
Georg Brandl8ec7f652007-08-15 14:28:01 +000029
30
31.. function:: copyfile(src, dst)
32
Georg Brandlec32b6b2008-01-06 16:12:39 +000033 Copy the contents (no metadata) of the file named *src* to a file named *dst*.
34 The destination location must be writable; otherwise, an :exc:`IOError` exception
Georg Brandl8ec7f652007-08-15 14:28:01 +000035 will be raised. If *dst* already exists, it will be replaced. Special files
36 such as character or block devices and pipes cannot be copied with this
37 function. *src* and *dst* are path names given as strings.
38
39
40.. function:: copyfileobj(fsrc, fdst[, length])
41
42 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
43 The integer *length*, if given, is the buffer size. In particular, a negative
44 *length* value means to copy the data without looping over the source data in
45 chunks; by default the data is read in chunks to avoid uncontrolled memory
46 consumption. Note that if the current file position of the *fsrc* object is not
47 0, only the contents from the current file position to the end of the file will
48 be copied.
49
50
51.. function:: copymode(src, dst)
52
53 Copy the permission bits from *src* to *dst*. The file contents, owner, and
54 group are unaffected. *src* and *dst* are path names given as strings.
55
56
57.. function:: copystat(src, dst)
58
59 Copy the permission bits, last access time, last modification time, and flags
60 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
61 and *dst* are path names given as strings.
62
63
64.. function:: copy(src, dst)
65
66 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
67 file with the same basename as *src* is created (or overwritten) in the
68 directory specified. Permission bits are copied. *src* and *dst* are path
69 names given as strings.
70
71
72.. function:: copy2(src, dst)
73
74 Similar to :func:`copy`, but last access time and last modification time are
75 copied as well. This is similar to the Unix command :program:`cp -p`.
76
77
78.. function:: copytree(src, dst[, symlinks])
79
80 Recursively copy an entire directory tree rooted at *src*. The destination
81 directory, named by *dst*, must not already exist; it will be created as well as
82 missing parent directories. Permissions and times of directories are copied with
83 :func:`copystat`, individual files are copied using :func:`copy2`. If
84 *symlinks* is true, symbolic links in the source tree are represented as
85 symbolic links in the new tree; if false or omitted, the contents of the linked
86 files are copied to the new tree. If exception(s) occur, an :exc:`Error` is
87 raised with a list of reasons.
88
Georg Brandlec32b6b2008-01-06 16:12:39 +000089 The source code for this should be considered an example rather than a tool.
Georg Brandl8ec7f652007-08-15 14:28:01 +000090
91 .. versionchanged:: 2.3
92 :exc:`Error` is raised if any exceptions occur during copying, rather than
93 printing a message.
94
95 .. versionchanged:: 2.5
96 Create intermediate directories needed to create *dst*, rather than raising an
97 error. Copy permissions and times of directories using :func:`copystat`.
98
99
100.. function:: rmtree(path[, ignore_errors[, onerror]])
101
102 .. index:: single: directory; deleting
103
104 Delete an entire directory tree (*path* must point to a directory). If
105 *ignore_errors* is true, errors resulting from failed removals will be ignored;
106 if false or omitted, such errors are handled by calling a handler specified by
107 *onerror* or, if that is omitted, they raise an exception.
108
109 If *onerror* is provided, it must be a callable that accepts three parameters:
110 *function*, *path*, and *excinfo*. The first parameter, *function*, is the
111 function which raised the exception; it will be :func:`os.listdir`,
112 :func:`os.remove` or :func:`os.rmdir`. The second parameter, *path*, will be
113 the path name passed to *function*. The third parameter, *excinfo*, will be the
114 exception information return by :func:`sys.exc_info`. Exceptions raised by
115 *onerror* will not be caught.
116
117
118.. function:: move(src, dst)
119
120 Recursively move a file or directory to another location.
121
Georg Brandlec32b6b2008-01-06 16:12:39 +0000122 If the destination is on the current filesystem, then simply use rename.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000123 Otherwise, copy src to the dst and then remove src.
124
125 .. versionadded:: 2.3
126
127
128.. exception:: Error
129
Georg Brandlec32b6b2008-01-06 16:12:39 +0000130 This exception collects exceptions that raised during a multi-file operation. For
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131 :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
132 *dstname*, *exception*).
133
134 .. versionadded:: 2.3
135
136
137.. _shutil-example:
138
139Example
140-------
141
142This example is the implementation of the :func:`copytree` function, described
143above, with the docstring omitted. It demonstrates many of the other functions
144provided by this module. ::
145
146 def copytree(src, dst, symlinks=False):
147 names = os.listdir(src)
148 os.makedirs(dst)
149 errors = []
150 for name in names:
151 srcname = os.path.join(src, name)
152 dstname = os.path.join(dst, name)
153 try:
154 if symlinks and os.path.islink(srcname):
155 linkto = os.readlink(srcname)
156 os.symlink(linkto, dstname)
157 elif os.path.isdir(srcname):
158 copytree(srcname, dstname, symlinks)
159 else:
160 copy2(srcname, dstname)
161 # XXX What about devices, sockets etc.?
162 except (IOError, os.error), why:
163 errors.append((srcname, dstname, str(why)))
164 # catch the Error from the recursive copytree so that we can
165 # continue with other files
166 except Error, err:
167 errors.extend(err.args[0])
168 try:
169 copystat(src, dst)
170 except WindowsError:
171 # can't copy file access times on Windows
172 pass
173 except OSError, why:
174 errors.extend((src, dst, str(why)))
175 if errors:
176 raise Error, errors