blob: 98b249f2ca22d06ad4b1f69e9a8bd9869d73ca58 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`shutil` --- High-level file operations
2============================================
3
4.. module:: shutil
5 :synopsis: High-level file operations, including copying.
6.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Georg Brandlb19be572007-12-29 10:57:00 +00007.. partly based on the docstrings
Georg Brandl8ec7f652007-08-15 14:28:01 +00008
9.. index::
10 single: file; copying
11 single: copying files
12
Éric Araujo29a0b572011-08-19 02:14:03 +020013**Source code:** :source:`Lib/shutil.py`
14
15--------------
16
Georg Brandl8ec7f652007-08-15 14:28:01 +000017The :mod:`shutil` module offers a number of high-level operations on files and
18collections of files. In particular, functions are provided which support file
Mark Summerfieldac3d4292007-11-02 08:24:59 +000019copying and removal. For operations on individual files, see also the
20:mod:`os` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
Georg Brandlbf863b12007-08-15 19:06:04 +000022.. warning::
Georg Brandlec32b6b2008-01-06 16:12:39 +000023
24 Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
25 can't copy all file metadata.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000026
Georg Brandlec32b6b2008-01-06 16:12:39 +000027 On POSIX platforms, this means that file owner and group are lost as well
Georg Brandl9af94982008-09-13 17:41:16 +000028 as ACLs. On Mac OS, the resource fork and other metadata are not used.
Georg Brandlec32b6b2008-01-06 16:12:39 +000029 This means that resources will be lost and file type and creator codes will
30 not be correct. On Windows, file owners, ACLs and alternate data streams
31 are not copied.
Georg Brandl8ec7f652007-08-15 14:28:01 +000032
Raymond Hettingere0e08222010-11-06 07:10:31 +000033
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000034Directory and files operations
35------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
Georg Brandl8ec7f652007-08-15 14:28:01 +000037.. function:: copyfileobj(fsrc, fdst[, length])
38
39 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
40 The integer *length*, if given, is the buffer size. In particular, a negative
41 *length* value means to copy the data without looping over the source data in
42 chunks; by default the data is read in chunks to avoid uncontrolled memory
43 consumption. Note that if the current file position of the *fsrc* object is not
44 0, only the contents from the current file position to the end of the file will
45 be copied.
46
47
Georg Brandl786ead62008-04-19 16:57:43 +000048.. function:: copyfile(src, dst)
49
50 Copy the contents (no metadata) of the file named *src* to a file named *dst*.
51 *dst* must be the complete target file name; look at :func:`copy` for a copy that
Georg Brandl905e0f62008-12-05 15:32:29 +000052 accepts a target directory path. If *src* and *dst* are the same files,
53 :exc:`Error` is raised.
Georg Brandl786ead62008-04-19 16:57:43 +000054 The destination location must be writable; otherwise, an :exc:`IOError` exception
55 will be raised. If *dst* already exists, it will be replaced. Special files
56 such as character or block devices and pipes cannot be copied with this
57 function. *src* and *dst* are path names given as strings.
58
59
Georg Brandl8ec7f652007-08-15 14:28:01 +000060.. function:: copymode(src, dst)
61
62 Copy the permission bits from *src* to *dst*. The file contents, owner, and
63 group are unaffected. *src* and *dst* are path names given as strings.
64
65
66.. function:: copystat(src, dst)
67
68 Copy the permission bits, last access time, last modification time, and flags
69 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
70 and *dst* are path names given as strings.
71
72
73.. function:: copy(src, dst)
74
75 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
76 file with the same basename as *src* is created (or overwritten) in the
77 directory specified. Permission bits are copied. *src* and *dst* are path
78 names given as strings.
79
80
81.. function:: copy2(src, dst)
82
Georg Brandl88107da2008-05-16 13:18:50 +000083 Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just
84 :func:`copy` followed by :func:`copystat`. This is similar to the
85 Unix command :program:`cp -p`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000086
87
Georg Brandle78fbcc2008-07-05 10:13:36 +000088.. function:: ignore_patterns(\*patterns)
89
90 This factory function creates a function that can be used as a callable for
91 :func:`copytree`\'s *ignore* argument, ignoring files and directories that
Andrew M. Kuchlingc4060842008-07-06 17:43:16 +000092 match one of the glob-style *patterns* provided. See the example below.
Georg Brandle78fbcc2008-07-05 10:13:36 +000093
94 .. versionadded:: 2.6
95
96
97.. function:: copytree(src, dst[, symlinks=False[, ignore=None]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000098
99 Recursively copy an entire directory tree rooted at *src*. The destination
Georg Brandle78fbcc2008-07-05 10:13:36 +0000100 directory, named by *dst*, must not already exist; it will be created as well
101 as missing parent directories. Permissions and times of directories are
102 copied with :func:`copystat`, individual files are copied using
103 :func:`copy2`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
Georg Brandle78fbcc2008-07-05 10:13:36 +0000105 If *symlinks* is true, symbolic links in the source tree are represented as
Senthil Kumaran22a7b482011-08-02 18:50:44 +0800106 symbolic links in the new tree, but the metadata of the original links is NOT
107 copied; if false or omitted, the contents and metadata of the linked files
108 are copied to the new tree.
Georg Brandle78fbcc2008-07-05 10:13:36 +0000109
110 If *ignore* is given, it must be a callable that will receive as its
111 arguments the directory being visited by :func:`copytree`, and a list of its
112 contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
113 called recursively, the *ignore* callable will be called once for each
114 directory that is copied. The callable must return a sequence of directory
115 and file names relative to the current directory (i.e. a subset of the items
116 in its second argument); these names will then be ignored in the copy
117 process. :func:`ignore_patterns` can be used to create such a callable that
118 ignores names based on glob-style patterns.
119
120 If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
121
122 The source code for this should be considered an example rather than the
123 ultimate tool.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
125 .. versionchanged:: 2.3
126 :exc:`Error` is raised if any exceptions occur during copying, rather than
127 printing a message.
128
129 .. versionchanged:: 2.5
130 Create intermediate directories needed to create *dst*, rather than raising an
131 error. Copy permissions and times of directories using :func:`copystat`.
132
Georg Brandle78fbcc2008-07-05 10:13:36 +0000133 .. versionchanged:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000134 Added the *ignore* argument to be able to influence what is being copied.
Georg Brandle78fbcc2008-07-05 10:13:36 +0000135
Georg Brandl8ec7f652007-08-15 14:28:01 +0000136
137.. function:: rmtree(path[, ignore_errors[, onerror]])
138
139 .. index:: single: directory; deleting
140
Georg Brandl52353982008-01-20 14:17:42 +0000141 Delete an entire directory tree; *path* must point to a directory (but not a
142 symbolic link to a directory). If *ignore_errors* is true, errors resulting
143 from failed removals will be ignored; if false or omitted, such errors are
144 handled by calling a handler specified by *onerror* or, if that is omitted,
145 they raise an exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
Georg Brandl52353982008-01-20 14:17:42 +0000147 If *onerror* is provided, it must be a callable that accepts three
148 parameters: *function*, *path*, and *excinfo*. The first parameter,
149 *function*, is the function which raised the exception; it will be
150 :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or
151 :func:`os.rmdir`. The second parameter, *path*, will be the path name passed
152 to *function*. The third parameter, *excinfo*, will be the exception
153 information return by :func:`sys.exc_info`. Exceptions raised by *onerror*
154 will not be caught.
155
156 .. versionchanged:: 2.6
157 Explicitly check for *path* being a symbolic link and raise :exc:`OSError`
158 in that case.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000159
160
161.. function:: move(src, dst)
162
Éric Araujod01aebe2011-07-29 12:10:53 +0200163 Recursively move a file or directory (*src*) to another location (*dst*).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164
Éric Araujod01aebe2011-07-29 12:10:53 +0200165 If the destination is a directory or a symlink to a directory, then *src* is
166 moved inside that directory.
167
168 The destination directory must not already exist. If the destination already
169 exists but is not a directory, it may be overwritten depending on
170 :func:`os.rename` semantics.
171
172 If the destination is on the current filesystem, then :func:`os.rename` is
173 used. Otherwise, *src* is copied (using :func:`copy2`) to *dst* and then
174 removed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000175
176 .. versionadded:: 2.3
177
178
179.. exception:: Error
180
Éric Araujod01aebe2011-07-29 12:10:53 +0200181 This exception collects exceptions that are raised during a multi-file
182 operation. For :func:`copytree`, the exception argument is a list of 3-tuples
183 (*srcname*, *dstname*, *exception*).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184
185 .. versionadded:: 2.3
186
Éric Araujod01aebe2011-07-29 12:10:53 +0200187
Georg Brandl8ec7f652007-08-15 14:28:01 +0000188.. _shutil-example:
189
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000190copytree example
191::::::::::::::::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000192
193This example is the implementation of the :func:`copytree` function, described
194above, with the docstring omitted. It demonstrates many of the other functions
195provided by this module. ::
196
Georg Brandle78fbcc2008-07-05 10:13:36 +0000197 def copytree(src, dst, symlinks=False, ignore=None):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000198 names = os.listdir(src)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000199 if ignore is not None:
200 ignored_names = ignore(src, names)
201 else:
202 ignored_names = set()
203
Georg Brandl8ec7f652007-08-15 14:28:01 +0000204 os.makedirs(dst)
205 errors = []
206 for name in names:
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000207 if name in ignored_names:
Georg Brandle78fbcc2008-07-05 10:13:36 +0000208 continue
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209 srcname = os.path.join(src, name)
210 dstname = os.path.join(dst, name)
211 try:
212 if symlinks and os.path.islink(srcname):
213 linkto = os.readlink(srcname)
214 os.symlink(linkto, dstname)
215 elif os.path.isdir(srcname):
Georg Brandle78fbcc2008-07-05 10:13:36 +0000216 copytree(srcname, dstname, symlinks, ignore)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217 else:
218 copy2(srcname, dstname)
219 # XXX What about devices, sockets etc.?
220 except (IOError, os.error), why:
221 errors.append((srcname, dstname, str(why)))
222 # catch the Error from the recursive copytree so that we can
223 # continue with other files
224 except Error, err:
225 errors.extend(err.args[0])
226 try:
227 copystat(src, dst)
228 except WindowsError:
229 # can't copy file access times on Windows
230 pass
231 except OSError, why:
232 errors.extend((src, dst, str(why)))
233 if errors:
Georg Brandlc1edec32009-06-03 07:25:35 +0000234 raise Error(errors)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000235
236Another example that uses the :func:`ignore_patterns` helper::
237
238 from shutil import copytree, ignore_patterns
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000239
Georg Brandle78fbcc2008-07-05 10:13:36 +0000240 copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
241
242This will copy everything except ``.pyc`` files and files or directories whose
243name starts with ``tmp``.
244
245Another example that uses the *ignore* argument to add a logging call::
246
247 from shutil import copytree
248 import logging
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000249
Georg Brandle78fbcc2008-07-05 10:13:36 +0000250 def _logpath(path, names):
251 logging.info('Working in %s' % path)
252 return [] # nothing will be ignored
253
254 copytree(source, destination, ignore=_logpath)
255
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000256
257Archives operations
258-------------------
259
260.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
261
262 Create an archive file (eg. zip or tar) and returns its name.
263
264 *base_name* is the name of the file to create, including the path, minus
265 any format-specific extension. *format* is the archive format: one of
Tarek Ziadée593fad2010-04-20 21:09:06 +0000266 "zip", "tar", "bztar" or "gztar".
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000267
268 *root_dir* is a directory that will be the root directory of the
269 archive; ie. we typically chdir into *root_dir* before creating the
270 archive.
271
272 *base_dir* is the directory where we start archiving from;
273 ie. *base_dir* will be the common prefix of all files and
274 directories in the archive.
275
276 *root_dir* and *base_dir* both default to the current directory.
277
278 *owner* and *group* are used when creating a tar archive. By default,
279 uses the current owner and group.
280
281 .. versionadded:: 2.7
282
283
284.. function:: get_archive_formats()
285
Éric Araujod01aebe2011-07-29 12:10:53 +0200286 Return a list of supported formats for archiving.
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000287 Each element of the returned sequence is a tuple ``(name, description)``
288
289 By default :mod:`shutil` provides these formats:
290
291 - *gztar*: gzip'ed tar-file
292 - *bztar*: bzip2'ed tar-file
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000293 - *tar*: uncompressed tar file
294 - *zip*: ZIP file
295
296 You can register new formats or provide your own archiver for any existing
297 formats, by using :func:`register_archive_format`.
298
299 .. versionadded:: 2.7
300
301
302.. function:: register_archive_format(name, function, [extra_args, [description]])
303
Éric Araujod01aebe2011-07-29 12:10:53 +0200304 Register an archiver for the format *name*. *function* is a callable that
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000305 will be used to invoke the archiver.
306
307 If given, *extra_args* is a sequence of ``(name, value)`` that will be
308 used as extra keywords arguments when the archiver callable is used.
309
310 *description* is used by :func:`get_archive_formats` which returns the
311 list of archivers. Defaults to an empty list.
312
313 .. versionadded:: 2.7
314
315
316.. function:: unregister_archive_format(name)
317
318 Remove the archive format *name* from the list of supported formats.
319
320 .. versionadded:: 2.7
321
322
323Archiving example
324:::::::::::::::::
325
326In this example, we create a gzip'ed tar-file archive containing all files
327found in the :file:`.ssh` directory of the user::
328
329 >>> from shutil import make_archive
330 >>> import os
331 >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
332 >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
333 >>> make_archive(archive_name, 'gztar', root_dir)
334 '/Users/tarek/myarchive.tar.gz'
335
336The resulting archive contains::
337
338 $ tar -tzvf /Users/tarek/myarchive.tar.gz
339 drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
340 -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
341 -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
342 -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
343 -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
344 -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
345 -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
346 -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
347
348