blob: 759dab132841df001c069e1648e73cc4c08a2627 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00007.. partly based on the docstrings
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index::
10 single: file; copying
11 single: copying files
12
Raymond Hettinger10480942011-01-10 03:26:08 +000013**Source code:** :source:`Lib/shutil.py`
14
Georg Brandl116aa622007-08-15 14:28:22 +000015The :mod:`shutil` module offers a number of high-level operations on files and
16collections of files. In particular, functions are provided which support file
Guido van Rossum2cc30da2007-11-02 23:46:40 +000017copying and removal. For operations on individual files, see also the
18:mod:`os` module.
Georg Brandl116aa622007-08-15 14:28:22 +000019
Guido van Rossumda27fd22007-08-17 00:24:54 +000020.. warning::
Christian Heimes7f044312008-01-06 17:05:40 +000021
22 Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
Raymond Hettinger10480942011-01-10 03:26:08 +000023 cannot copy all file metadata.
Georg Brandl48310cd2009-01-03 21:18:54 +000024
Christian Heimes7f044312008-01-06 17:05:40 +000025 On POSIX platforms, this means that file owner and group are lost as well
Georg Brandlc575c902008-09-13 17:46:05 +000026 as ACLs. On Mac OS, the resource fork and other metadata are not used.
Christian Heimes7f044312008-01-06 17:05:40 +000027 This means that resources will be lost and file type and creator codes will
28 not be correct. On Windows, file owners, ACLs and alternate data streams
29 are not copied.
Georg Brandl116aa622007-08-15 14:28:22 +000030
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000031
Tarek Ziadé396fad72010-02-23 05:30:31 +000032Directory and files operations
33------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000034
Georg Brandl116aa622007-08-15 14:28:22 +000035.. function:: copyfileobj(fsrc, fdst[, length])
36
37 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
38 The integer *length*, if given, is the buffer size. In particular, a negative
39 *length* value means to copy the data without looping over the source data in
40 chunks; by default the data is read in chunks to avoid uncontrolled memory
41 consumption. Note that if the current file position of the *fsrc* object is not
42 0, only the contents from the current file position to the end of the file will
43 be copied.
44
45
Christian Heimesa342c012008-04-20 21:01:16 +000046.. function:: copyfile(src, dst)
47
48 Copy the contents (no metadata) of the file named *src* to a file named *dst*.
49 *dst* must be the complete target file name; look at :func:`copy` for a copy that
Georg Brandlaf265f42008-12-07 15:06:20 +000050 accepts a target directory path. If *src* and *dst* are the same files,
51 :exc:`Error` is raised.
Christian Heimesa342c012008-04-20 21:01:16 +000052 The destination location must be writable; otherwise, an :exc:`IOError` exception
53 will be raised. If *dst* already exists, it will be replaced. Special files
54 such as character or block devices and pipes cannot be copied with this
55 function. *src* and *dst* are path names given as strings.
56
57
Georg Brandl116aa622007-08-15 14:28:22 +000058.. function:: copymode(src, dst)
59
60 Copy the permission bits from *src* to *dst*. The file contents, owner, and
61 group are unaffected. *src* and *dst* are path names given as strings.
62
63
64.. function:: copystat(src, dst)
65
66 Copy the permission bits, last access time, last modification time, and flags
67 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
68 and *dst* are path names given as strings.
69
70
71.. function:: copy(src, dst)
72
73 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
74 file with the same basename as *src* is created (or overwritten) in the
75 directory specified. Permission bits are copied. *src* and *dst* are path
76 names given as strings.
77
78
79.. function:: copy2(src, dst)
80
Alexandre Vassalottibee32532008-05-16 18:15:12 +000081 Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just
82 :func:`copy` followed by :func:`copystat`. This is similar to the
83 Unix command :program:`cp -p`.
Georg Brandl116aa622007-08-15 14:28:22 +000084
85
Georg Brandl86b2fb92008-07-16 03:43:04 +000086.. function:: ignore_patterns(\*patterns)
87
88 This factory function creates a function that can be used as a callable for
89 :func:`copytree`\'s *ignore* argument, ignoring files and directories that
90 match one of the glob-style *patterns* provided. See the example below.
91
92
Ezio Melotticb999a32010-04-20 11:26:51 +000093.. function:: copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +000094
95 Recursively copy an entire directory tree rooted at *src*. The destination
Georg Brandl86b2fb92008-07-16 03:43:04 +000096 directory, named by *dst*, must not already exist; it will be created as well
97 as missing parent directories. Permissions and times of directories are
98 copied with :func:`copystat`, individual files are copied using
99 :func:`copy2`.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Georg Brandl86b2fb92008-07-16 03:43:04 +0000101 If *symlinks* is true, symbolic links in the source tree are represented as
102 symbolic links in the new tree; if false or omitted, the contents of the
103 linked files are copied to the new tree.
104
Tarek Ziadéfb437512010-04-20 08:57:33 +0000105 When *symlinks* is false, if the file pointed by the symlink doesn't
106 exist, a exception will be added in the list of errors raised in
107 a :exc:`Error` exception at the end of the copy process.
108 You can set the optional *ignore_dangling_symlinks* flag to true if you
Tarek Ziadé8c26c7d2010-04-23 13:03:50 +0000109 want to silence this exception. Notice that this option has no effect
110 on platforms that don't support :func:`os.symlink`.
Tarek Ziadéfb437512010-04-20 08:57:33 +0000111
Georg Brandl86b2fb92008-07-16 03:43:04 +0000112 If *ignore* is given, it must be a callable that will receive as its
113 arguments the directory being visited by :func:`copytree`, and a list of its
114 contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
115 called recursively, the *ignore* callable will be called once for each
116 directory that is copied. The callable must return a sequence of directory
117 and file names relative to the current directory (i.e. a subset of the items
118 in its second argument); these names will then be ignored in the copy
119 process. :func:`ignore_patterns` can be used to create such a callable that
120 ignores names based on glob-style patterns.
121
122 If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
123
Tarek Ziadé5340db32010-04-19 22:30:51 +0000124 If *copy_function* is given, it must be a callable that will be used
125 to copy each file. It will be called with the source path and the
126 destination path as arguments. By default, :func:`copy2` is used, but any
127 function that supports the same signature (like :func:`copy`) can be used.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
Tarek Ziadé5340db32010-04-19 22:30:51 +0000129 .. versionchanged:: 3.2
130 Added the *copy_function* argument to be able to provide a custom copy
131 function.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Ezio Melotticb999a32010-04-20 11:26:51 +0000133 .. versionchanged:: 3.2
Tarek Ziadéfb437512010-04-20 08:57:33 +0000134 Added the *ignore_dangling_symlinks* argument to silent dangling symlinks
135 errors when *symlinks* is false.
136
137
Georg Brandl18244152009-09-02 20:34:52 +0000138.. function:: rmtree(path, ignore_errors=False, onerror=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140 .. index:: single: directory; deleting
141
Christian Heimes9bd667a2008-01-20 15:14:11 +0000142 Delete an entire directory tree; *path* must point to a directory (but not a
143 symbolic link to a directory). If *ignore_errors* is true, errors resulting
144 from failed removals will be ignored; if false or omitted, such errors are
145 handled by calling a handler specified by *onerror* or, if that is omitted,
146 they raise an exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Christian Heimes9bd667a2008-01-20 15:14:11 +0000148 If *onerror* is provided, it must be a callable that accepts three
149 parameters: *function*, *path*, and *excinfo*. The first parameter,
150 *function*, is the function which raised the exception; it will be
151 :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or
152 :func:`os.rmdir`. The second parameter, *path*, will be the path name passed
153 to *function*. The third parameter, *excinfo*, will be the exception
154 information return by :func:`sys.exc_info`. Exceptions raised by *onerror*
155 will not be caught.
156
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158.. function:: move(src, dst)
159
160 Recursively move a file or directory to another location.
161
Christian Heimes7f044312008-01-06 17:05:40 +0000162 If the destination is on the current filesystem, then simply use rename.
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000163 Otherwise, copy src (with :func:`copy2`) to the dst and then remove src.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166.. exception:: Error
167
Christian Heimes7f044312008-01-06 17:05:40 +0000168 This exception collects exceptions that raised during a multi-file operation. For
Georg Brandl116aa622007-08-15 14:28:22 +0000169 :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
170 *dstname*, *exception*).
171
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173.. _shutil-example:
174
Tarek Ziadé396fad72010-02-23 05:30:31 +0000175copytree example
176::::::::::::::::
Georg Brandl116aa622007-08-15 14:28:22 +0000177
178This example is the implementation of the :func:`copytree` function, described
179above, with the docstring omitted. It demonstrates many of the other functions
180provided by this module. ::
181
182 def copytree(src, dst, symlinks=False):
183 names = os.listdir(src)
184 os.makedirs(dst)
185 errors = []
186 for name in names:
187 srcname = os.path.join(src, name)
188 dstname = os.path.join(dst, name)
189 try:
190 if symlinks and os.path.islink(srcname):
191 linkto = os.readlink(srcname)
192 os.symlink(linkto, dstname)
193 elif os.path.isdir(srcname):
194 copytree(srcname, dstname, symlinks)
195 else:
196 copy2(srcname, dstname)
197 # XXX What about devices, sockets etc.?
198 except (IOError, os.error) as why:
199 errors.append((srcname, dstname, str(why)))
200 # catch the Error from the recursive copytree so that we can
201 # continue with other files
202 except Error as err:
203 errors.extend(err.args[0])
204 try:
205 copystat(src, dst)
206 except WindowsError:
207 # can't copy file access times on Windows
208 pass
209 except OSError as why:
210 errors.extend((src, dst, str(why)))
211 if errors:
Collin Winterc79461b2007-09-01 23:34:30 +0000212 raise Error(errors)
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Tarek Ziadé396fad72010-02-23 05:30:31 +0000214Another example that uses the :func:`ignore_patterns` helper::
215
216 from shutil import copytree, ignore_patterns
217
218 copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
219
220This will copy everything except ``.pyc`` files and files or directories whose
221name starts with ``tmp``.
222
223Another example that uses the *ignore* argument to add a logging call::
224
225 from shutil import copytree
226 import logging
227
228 def _logpath(path, names):
229 logging.info('Working in %s' % path)
230 return [] # nothing will be ignored
231
232 copytree(source, destination, ignore=_logpath)
233
234
235Archives operations
236-------------------
237
238.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
239
Ezio Melotticb999a32010-04-20 11:26:51 +0000240 Create an archive file (e.g. zip or tar) and returns its name.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000241
242 *base_name* is the name of the file to create, including the path, minus
243 any format-specific extension. *format* is the archive format: one of
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000244 "zip", "tar", "bztar" (if the :mod:`bz2` module is available) or "gztar".
Tarek Ziadé396fad72010-02-23 05:30:31 +0000245
246 *root_dir* is a directory that will be the root directory of the
Ezio Melotticb999a32010-04-20 11:26:51 +0000247 archive; i.e. we typically chdir into *root_dir* before creating the
Tarek Ziadé396fad72010-02-23 05:30:31 +0000248 archive.
249
250 *base_dir* is the directory where we start archiving from;
Ezio Melotticb999a32010-04-20 11:26:51 +0000251 i.e. *base_dir* will be the common prefix of all files and
Tarek Ziadé396fad72010-02-23 05:30:31 +0000252 directories in the archive.
253
254 *root_dir* and *base_dir* both default to the current directory.
255
256 *owner* and *group* are used when creating a tar archive. By default,
257 uses the current owner and group.
258
Ezio Melottif8754a62010-03-21 07:16:43 +0000259 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000260
261
262.. function:: get_archive_formats()
263
264 Returns a list of supported formats for archiving.
265 Each element of the returned sequence is a tuple ``(name, description)``
266
267 By default :mod:`shutil` provides these formats:
268
269 - *gztar*: gzip'ed tar-file
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000270 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000271 - *tar*: uncompressed tar file
272 - *zip*: ZIP file
273
274 You can register new formats or provide your own archiver for any existing
275 formats, by using :func:`register_archive_format`.
276
Ezio Melottif8754a62010-03-21 07:16:43 +0000277 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000278
279
280.. function:: register_archive_format(name, function, [extra_args, [description]])
281
282 Registers an archiver for the format *name*. *function* is a callable that
283 will be used to invoke the archiver.
284
285 If given, *extra_args* is a sequence of ``(name, value)`` that will be
286 used as extra keywords arguments when the archiver callable is used.
287
288 *description* is used by :func:`get_archive_formats` which returns the
289 list of archivers. Defaults to an empty list.
290
Ezio Melottif8754a62010-03-21 07:16:43 +0000291 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000292
293
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000294.. function:: unregister_archive_format(name)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000295
296 Remove the archive format *name* from the list of supported formats.
297
Ezio Melottif8754a62010-03-21 07:16:43 +0000298 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000299
300
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000301.. function:: unpack_archive(filename[, extract_dir[, format]])
302
303 Unpack an archive. *filename* is the full path of the archive.
304
305 *extract_dir* is the name of the target directory where the archive is
306 unpacked. If not provided, the current working directory is used.
307
308 *format* is the archive format: one of "zip", "tar", or "gztar". Or any
309 other format registered with :func:`register_unpack_format`. If not
310 provided, :func:`unpack_archive` will use the archive file name extension
311 and see if an unpacker was registered for that extension. In case none is
312 found, a :exc:`ValueError` is raised.
313
314 .. versionadded:: 3.2
315
316
317.. function:: register_unpack_format(name, extensions, function[, extra_args[,description]])
318
319 Registers an unpack format. *name* is the name of the format and
320 *extensions* is a list of extensions corresponding to the format, like
321 ``.zip`` for Zip files.
322
323 *function* is the callable that will be used to unpack archives. The
324 callable will receive the path of the archive, followed by the directory
325 the archive must be extracted to.
326
327 When provided, *extra_args* is a sequence of ``(name, value)`` tuples that
328 will be passed as keywords arguments to the callable.
329
330 *description* can be provided to describe the format, and will be returned
331 by the :func:`get_unpack_formats` function.
332
333 .. versionadded:: 3.2
334
335
336.. function:: unregister_unpack_format(name)
337
338 Unregister an unpack format. *name* is the name of the format.
339
340 .. versionadded:: 3.2
341
342
343.. function:: get_unpack_formats()
344
345 Return a list of all registered formats for unpacking.
346 Each element of the returned sequence is a tuple
347 ``(name, extensions, description)``.
348
349 By default :mod:`shutil` provides these formats:
350
351 - *gztar*: gzip'ed tar-file
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000352 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000353 - *tar*: uncompressed tar file
354 - *zip*: ZIP file
355
356 You can register new formats or provide your own unpacker for any existing
357 formats, by using :func:`register_unpack_format`.
358
359 .. versionadded:: 3.2
360
361
362
Tarek Ziadé396fad72010-02-23 05:30:31 +0000363Archiving example
364:::::::::::::::::
365
366In this example, we create a gzip'ed tar-file archive containing all files
367found in the :file:`.ssh` directory of the user::
368
369 >>> from shutil import make_archive
370 >>> import os
371 >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
372 >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
373 >>> make_archive(archive_name, 'gztar', root_dir)
374 '/Users/tarek/myarchive.tar.gz'
375
376The resulting archive contains::
377
378 $ tar -tzvf /Users/tarek/myarchive.tar.gz
379 drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
380 -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
381 -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
382 -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
383 -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
384 -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
385 -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
386 -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
387
388