blob: bd66aa9621633a23487edd410a595fb3a1d05356 [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
Georg Brandl8ec7f652007-08-15 14:28:01 +000031.. function:: copyfileobj(fsrc, fdst[, length])
32
33 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
34 The integer *length*, if given, is the buffer size. In particular, a negative
35 *length* value means to copy the data without looping over the source data in
36 chunks; by default the data is read in chunks to avoid uncontrolled memory
37 consumption. Note that if the current file position of the *fsrc* object is not
38 0, only the contents from the current file position to the end of the file will
39 be copied.
40
41
Georg Brandl786ead62008-04-19 16:57:43 +000042.. function:: copyfile(src, dst)
43
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.
47 The destination location must be writable; otherwise, an :exc:`IOError` exception
48 will be raised. If *dst* already exists, it will be replaced. Special files
49 such as character or block devices and pipes cannot be copied with this
50 function. *src* and *dst* are path names given as strings.
51
52
Georg Brandl8ec7f652007-08-15 14:28:01 +000053.. function:: copymode(src, dst)
54
55 Copy the permission bits from *src* to *dst*. The file contents, owner, and
56 group are unaffected. *src* and *dst* are path names given as strings.
57
58
59.. function:: copystat(src, dst)
60
61 Copy the permission bits, last access time, last modification time, and flags
62 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
63 and *dst* are path names given as strings.
64
65
66.. function:: copy(src, dst)
67
68 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
69 file with the same basename as *src* is created (or overwritten) in the
70 directory specified. Permission bits are copied. *src* and *dst* are path
71 names given as strings.
72
73
74.. function:: copy2(src, dst)
75
Georg Brandl88107da2008-05-16 13:18:50 +000076 Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just
77 :func:`copy` followed by :func:`copystat`. This is similar to the
78 Unix command :program:`cp -p`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80
81.. function:: copytree(src, dst[, symlinks])
82
83 Recursively copy an entire directory tree rooted at *src*. The destination
84 directory, named by *dst*, must not already exist; it will be created as well as
85 missing parent directories. Permissions and times of directories are copied with
86 :func:`copystat`, individual files are copied using :func:`copy2`. If
87 *symlinks* is true, symbolic links in the source tree are represented as
88 symbolic links in the new tree; if false or omitted, the contents of the linked
89 files are copied to the new tree. If exception(s) occur, an :exc:`Error` is
90 raised with a list of reasons.
91
Georg Brandlec32b6b2008-01-06 16:12:39 +000092 The source code for this should be considered an example rather than a tool.
Georg Brandl8ec7f652007-08-15 14:28:01 +000093
94 .. versionchanged:: 2.3
95 :exc:`Error` is raised if any exceptions occur during copying, rather than
96 printing a message.
97
98 .. versionchanged:: 2.5
99 Create intermediate directories needed to create *dst*, rather than raising an
100 error. Copy permissions and times of directories using :func:`copystat`.
101
102
103.. function:: rmtree(path[, ignore_errors[, onerror]])
104
105 .. index:: single: directory; deleting
106
Georg Brandl52353982008-01-20 14:17:42 +0000107 Delete an entire directory tree; *path* must point to a directory (but not a
108 symbolic link to a directory). If *ignore_errors* is true, errors resulting
109 from failed removals will be ignored; if false or omitted, such errors are
110 handled by calling a handler specified by *onerror* or, if that is omitted,
111 they raise an exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
Georg Brandl52353982008-01-20 14:17:42 +0000113 If *onerror* is provided, it must be a callable that accepts three
114 parameters: *function*, *path*, and *excinfo*. The first parameter,
115 *function*, is the function which raised the exception; it will be
116 :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or
117 :func:`os.rmdir`. The second parameter, *path*, will be the path name passed
118 to *function*. The third parameter, *excinfo*, will be the exception
119 information return by :func:`sys.exc_info`. Exceptions raised by *onerror*
120 will not be caught.
121
122 .. versionchanged:: 2.6
123 Explicitly check for *path* being a symbolic link and raise :exc:`OSError`
124 in that case.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125
126
127.. function:: move(src, dst)
128
129 Recursively move a file or directory to another location.
130
Georg Brandlec32b6b2008-01-06 16:12:39 +0000131 If the destination is on the current filesystem, then simply use rename.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132 Otherwise, copy src to the dst and then remove src.
133
134 .. versionadded:: 2.3
135
136
137.. exception:: Error
138
Georg Brandlec32b6b2008-01-06 16:12:39 +0000139 This exception collects exceptions that raised during a multi-file operation. For
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140 :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
141 *dstname*, *exception*).
142
143 .. versionadded:: 2.3
144
145
146.. _shutil-example:
147
148Example
149-------
150
151This example is the implementation of the :func:`copytree` function, described
152above, with the docstring omitted. It demonstrates many of the other functions
153provided by this module. ::
154
155 def copytree(src, dst, symlinks=False):
156 names = os.listdir(src)
157 os.makedirs(dst)
158 errors = []
159 for name in names:
160 srcname = os.path.join(src, name)
161 dstname = os.path.join(dst, name)
162 try:
163 if symlinks and os.path.islink(srcname):
164 linkto = os.readlink(srcname)
165 os.symlink(linkto, dstname)
166 elif os.path.isdir(srcname):
167 copytree(srcname, dstname, symlinks)
168 else:
169 copy2(srcname, dstname)
170 # XXX What about devices, sockets etc.?
171 except (IOError, os.error), why:
172 errors.append((srcname, dstname, str(why)))
173 # catch the Error from the recursive copytree so that we can
174 # continue with other files
175 except Error, err:
176 errors.extend(err.args[0])
177 try:
178 copystat(src, dst)
179 except WindowsError:
180 # can't copy file access times on Windows
181 pass
182 except OSError, why:
183 errors.extend((src, dst, str(why)))
184 if errors:
185 raise Error, errors