blob: 18f648518419dd4a28e9c90bb6e3b970e9abea3a [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
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000015--------------
16
Georg Brandl116aa622007-08-15 14:28:22 +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
Guido van Rossum2cc30da2007-11-02 23:46:40 +000019copying and removal. For operations on individual files, see also the
20:mod:`os` module.
Georg Brandl116aa622007-08-15 14:28:22 +000021
Guido van Rossumda27fd22007-08-17 00:24:54 +000022.. warning::
Christian Heimes7f044312008-01-06 17:05:40 +000023
Senthil Kumaran7f728c12012-02-13 23:30:47 +080024 Even the higher-level file copying functions (:func:`shutil.copy`,
25 :func:`shutil.copy2`) cannot copy all file metadata.
Georg Brandl48310cd2009-01-03 21:18:54 +000026
Christian Heimes7f044312008-01-06 17:05:40 +000027 On POSIX platforms, this means that file owner and group are lost as well
Georg Brandlc575c902008-09-13 17:46:05 +000028 as ACLs. On Mac OS, the resource fork and other metadata are not used.
Christian Heimes7f044312008-01-06 17:05:40 +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 Brandl116aa622007-08-15 14:28:22 +000032
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000033
Éric Araujof2fbb9c2012-01-16 16:55:55 +010034.. _file-operations:
35
Tarek Ziadé396fad72010-02-23 05:30:31 +000036Directory and files operations
37------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000038
Georg Brandl116aa622007-08-15 14:28:22 +000039.. function:: copyfileobj(fsrc, fdst[, length])
40
41 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
42 The integer *length*, if given, is the buffer size. In particular, a negative
43 *length* value means to copy the data without looping over the source data in
44 chunks; by default the data is read in chunks to avoid uncontrolled memory
45 consumption. Note that if the current file position of the *fsrc* object is not
46 0, only the contents from the current file position to the end of the file will
47 be copied.
48
49
Christian Heimesa342c012008-04-20 21:01:16 +000050.. function:: copyfile(src, dst)
51
Senthil Kumaran7f728c12012-02-13 23:30:47 +080052 Copy the contents (no metadata) of the file named *src* to a file named
53 *dst*. *dst* must be the complete target file name; look at
54 :func:`shutil.copy` for a copy that accepts a target directory path. If
55 *src* and *dst* are the same files, :exc:`Error` is raised.
Christian Heimesa342c012008-04-20 21:01:16 +000056 The destination location must be writable; otherwise, an :exc:`IOError` exception
57 will be raised. If *dst* already exists, it will be replaced. Special files
58 such as character or block devices and pipes cannot be copied with this
59 function. *src* and *dst* are path names given as strings.
60
61
Georg Brandl116aa622007-08-15 14:28:22 +000062.. function:: copymode(src, dst)
63
64 Copy the permission bits from *src* to *dst*. The file contents, owner, and
65 group are unaffected. *src* and *dst* are path names given as strings.
66
67
68.. function:: copystat(src, dst)
69
70 Copy the permission bits, last access time, last modification time, and flags
71 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
72 and *dst* are path names given as strings.
73
74
75.. function:: copy(src, dst)
76
77 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
78 file with the same basename as *src* is created (or overwritten) in the
79 directory specified. Permission bits are copied. *src* and *dst* are path
80 names given as strings.
81
82
83.. function:: copy2(src, dst)
84
Senthil Kumaran7f728c12012-02-13 23:30:47 +080085 Similar to :func:`shutil.copy`, but metadata is copied as well -- in fact,
86 this is just :func:`shutil.copy` followed by :func:`copystat`. This is
87 similar to the Unix command :program:`cp -p`.
Georg Brandl116aa622007-08-15 14:28:22 +000088
89
Georg Brandl86b2fb92008-07-16 03:43:04 +000090.. function:: ignore_patterns(\*patterns)
91
92 This factory function creates a function that can be used as a callable for
93 :func:`copytree`\'s *ignore* argument, ignoring files and directories that
94 match one of the glob-style *patterns* provided. See the example below.
95
96
Ezio Melotticb999a32010-04-20 11:26:51 +000097.. function:: copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +000098
99 Recursively copy an entire directory tree rooted at *src*. The destination
Senthil Kumaran7f728c12012-02-13 23:30:47 +0800100 directory, named by *dst*, must not already exist; it will be created as
101 well as missing parent directories. Permissions and times of directories
102 are copied with :func:`copystat`, individual files are copied using
103 :func:`shutil.copy2`.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Georg Brandl86b2fb92008-07-16 03:43:04 +0000105 If *symlinks* is true, symbolic links in the source tree are represented as
Senthil Kumaranef5c7162011-08-02 18:52:28 +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 Brandl86b2fb92008-07-16 03:43:04 +0000109
Tarek Ziadéfb437512010-04-20 08:57:33 +0000110 When *symlinks* is false, if the file pointed by the symlink doesn't
111 exist, a exception will be added in the list of errors raised in
112 a :exc:`Error` exception at the end of the copy process.
113 You can set the optional *ignore_dangling_symlinks* flag to true if you
Tarek Ziadé8c26c7d2010-04-23 13:03:50 +0000114 want to silence this exception. Notice that this option has no effect
115 on platforms that don't support :func:`os.symlink`.
Tarek Ziadéfb437512010-04-20 08:57:33 +0000116
Georg Brandl86b2fb92008-07-16 03:43:04 +0000117 If *ignore* is given, it must be a callable that will receive as its
118 arguments the directory being visited by :func:`copytree`, and a list of its
119 contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
120 called recursively, the *ignore* callable will be called once for each
121 directory that is copied. The callable must return a sequence of directory
122 and file names relative to the current directory (i.e. a subset of the items
123 in its second argument); these names will then be ignored in the copy
124 process. :func:`ignore_patterns` can be used to create such a callable that
125 ignores names based on glob-style patterns.
126
127 If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
128
Senthil Kumaran7f728c12012-02-13 23:30:47 +0800129 If *copy_function* is given, it must be a callable that will be used to copy
130 each file. It will be called with the source path and the destination path
131 as arguments. By default, :func:`shutil.copy2` is used, but any function
132 that supports the same signature (like :func:`copy`) can be used.
Georg Brandl116aa622007-08-15 14:28:22 +0000133
Tarek Ziadé5340db32010-04-19 22:30:51 +0000134 .. versionchanged:: 3.2
135 Added the *copy_function* argument to be able to provide a custom copy
136 function.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Ezio Melotticb999a32010-04-20 11:26:51 +0000138 .. versionchanged:: 3.2
Tarek Ziadéfb437512010-04-20 08:57:33 +0000139 Added the *ignore_dangling_symlinks* argument to silent dangling symlinks
140 errors when *symlinks* is false.
141
142
Georg Brandl18244152009-09-02 20:34:52 +0000143.. function:: rmtree(path, ignore_errors=False, onerror=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145 .. index:: single: directory; deleting
146
Christian Heimes9bd667a2008-01-20 15:14:11 +0000147 Delete an entire directory tree; *path* must point to a directory (but not a
148 symbolic link to a directory). If *ignore_errors* is true, errors resulting
149 from failed removals will be ignored; if false or omitted, such errors are
150 handled by calling a handler specified by *onerror* or, if that is omitted,
151 they raise an exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Christian Heimes9bd667a2008-01-20 15:14:11 +0000153 If *onerror* is provided, it must be a callable that accepts three
154 parameters: *function*, *path*, and *excinfo*. The first parameter,
155 *function*, is the function which raised the exception; it will be
156 :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or
157 :func:`os.rmdir`. The second parameter, *path*, will be the path name passed
158 to *function*. The third parameter, *excinfo*, will be the exception
159 information return by :func:`sys.exc_info`. Exceptions raised by *onerror*
160 will not be caught.
161
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163.. function:: move(src, dst)
164
Éric Araujo14382dc2011-07-28 22:49:11 +0200165 Recursively move a file or directory (*src*) to another location (*dst*).
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Éric Araujo14382dc2011-07-28 22:49:11 +0200167 If the destination is a directory or a symlink to a directory, then *src* is
168 moved inside that directory.
169
170 The destination directory must not already exist. If the destination already
171 exists but is not a directory, it may be overwritten depending on
172 :func:`os.rename` semantics.
173
174 If the destination is on the current filesystem, then :func:`os.rename` is
Senthil Kumaran7f728c12012-02-13 23:30:47 +0800175 used. Otherwise, *src* is copied (using :func:`shutil.copy2`) to *dst* and
176 then removed.
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179.. exception:: Error
180
Éric Araujo14382dc2011-07-28 22:49:11 +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 Brandl116aa622007-08-15 14:28:22 +0000184
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Éric Araujof2fbb9c2012-01-16 16:55:55 +0100186.. _shutil-copytree-example:
Georg Brandl116aa622007-08-15 14:28:22 +0000187
Tarek Ziadé396fad72010-02-23 05:30:31 +0000188copytree example
189::::::::::::::::
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191This example is the implementation of the :func:`copytree` function, described
192above, with the docstring omitted. It demonstrates many of the other functions
193provided by this module. ::
194
195 def copytree(src, dst, symlinks=False):
196 names = os.listdir(src)
197 os.makedirs(dst)
198 errors = []
199 for name in names:
200 srcname = os.path.join(src, name)
201 dstname = os.path.join(dst, name)
202 try:
203 if symlinks and os.path.islink(srcname):
204 linkto = os.readlink(srcname)
205 os.symlink(linkto, dstname)
206 elif os.path.isdir(srcname):
207 copytree(srcname, dstname, symlinks)
208 else:
209 copy2(srcname, dstname)
210 # XXX What about devices, sockets etc.?
211 except (IOError, os.error) as why:
212 errors.append((srcname, dstname, str(why)))
213 # catch the Error from the recursive copytree so that we can
214 # continue with other files
215 except Error as err:
216 errors.extend(err.args[0])
217 try:
218 copystat(src, dst)
219 except WindowsError:
220 # can't copy file access times on Windows
221 pass
222 except OSError as why:
223 errors.extend((src, dst, str(why)))
224 if errors:
Collin Winterc79461b2007-09-01 23:34:30 +0000225 raise Error(errors)
Georg Brandl116aa622007-08-15 14:28:22 +0000226
Tarek Ziadé396fad72010-02-23 05:30:31 +0000227Another example that uses the :func:`ignore_patterns` helper::
228
229 from shutil import copytree, ignore_patterns
230
231 copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
232
233This will copy everything except ``.pyc`` files and files or directories whose
234name starts with ``tmp``.
235
236Another example that uses the *ignore* argument to add a logging call::
237
238 from shutil import copytree
239 import logging
240
241 def _logpath(path, names):
242 logging.info('Working in %s' % path)
243 return [] # nothing will be ignored
244
245 copytree(source, destination, ignore=_logpath)
246
247
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000248.. _archiving-operations:
249
250Archiving operations
251--------------------
Tarek Ziadé396fad72010-02-23 05:30:31 +0000252
Éric Araujof2fbb9c2012-01-16 16:55:55 +0100253High-level utilities to create and read compressed and archived files are also
254provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
255
Tarek Ziadé396fad72010-02-23 05:30:31 +0000256.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
257
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000258 Create an archive file (such as zip or tar) and return its name.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000259
260 *base_name* is the name of the file to create, including the path, minus
261 any format-specific extension. *format* is the archive format: one of
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000262 "zip", "tar", "bztar" (if the :mod:`bz2` module is available) or "gztar".
Tarek Ziadé396fad72010-02-23 05:30:31 +0000263
264 *root_dir* is a directory that will be the root directory of the
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000265 archive; for example, we typically chdir into *root_dir* before creating the
Tarek Ziadé396fad72010-02-23 05:30:31 +0000266 archive.
267
268 *base_dir* is the directory where we start archiving from;
Ezio Melotticb999a32010-04-20 11:26:51 +0000269 i.e. *base_dir* will be the common prefix of all files and
Tarek Ziadé396fad72010-02-23 05:30:31 +0000270 directories in the archive.
271
272 *root_dir* and *base_dir* both default to the current directory.
273
274 *owner* and *group* are used when creating a tar archive. By default,
275 uses the current owner and group.
276
Éric Araujo06c42a32011-11-07 17:31:07 +0100277 *logger* must be an object compatible with :pep:`282`, usually an instance of
278 :class:`logging.Logger`.
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000279
Ezio Melottif8754a62010-03-21 07:16:43 +0000280 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000281
282
283.. function:: get_archive_formats()
284
Éric Araujo14382dc2011-07-28 22:49:11 +0200285 Return a list of supported formats for archiving.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000286 Each element of the returned sequence is a tuple ``(name, description)``
287
288 By default :mod:`shutil` provides these formats:
289
290 - *gztar*: gzip'ed tar-file
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000291 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000292 - *tar*: uncompressed tar file
293 - *zip*: ZIP file
294
295 You can register new formats or provide your own archiver for any existing
296 formats, by using :func:`register_archive_format`.
297
Ezio Melottif8754a62010-03-21 07:16:43 +0000298 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000299
300
301.. function:: register_archive_format(name, function, [extra_args, [description]])
302
Éric Araujo14382dc2011-07-28 22:49:11 +0200303 Register an archiver for the format *name*. *function* is a callable that
Tarek Ziadé396fad72010-02-23 05:30:31 +0000304 will be used to invoke the archiver.
305
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000306 If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be
Tarek Ziadé396fad72010-02-23 05:30:31 +0000307 used as extra keywords arguments when the archiver callable is used.
308
309 *description* is used by :func:`get_archive_formats` which returns the
310 list of archivers. Defaults to an empty list.
311
Ezio Melottif8754a62010-03-21 07:16:43 +0000312 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000313
314
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000315.. function:: unregister_archive_format(name)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000316
317 Remove the archive format *name* from the list of supported formats.
318
Ezio Melottif8754a62010-03-21 07:16:43 +0000319 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000320
321
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000322.. function:: unpack_archive(filename[, extract_dir[, format]])
323
324 Unpack an archive. *filename* is the full path of the archive.
325
326 *extract_dir* is the name of the target directory where the archive is
327 unpacked. If not provided, the current working directory is used.
328
329 *format* is the archive format: one of "zip", "tar", or "gztar". Or any
330 other format registered with :func:`register_unpack_format`. If not
331 provided, :func:`unpack_archive` will use the archive file name extension
332 and see if an unpacker was registered for that extension. In case none is
333 found, a :exc:`ValueError` is raised.
334
335 .. versionadded:: 3.2
336
337
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000338.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000339
340 Registers an unpack format. *name* is the name of the format and
341 *extensions* is a list of extensions corresponding to the format, like
342 ``.zip`` for Zip files.
343
344 *function* is the callable that will be used to unpack archives. The
345 callable will receive the path of the archive, followed by the directory
346 the archive must be extracted to.
347
348 When provided, *extra_args* is a sequence of ``(name, value)`` tuples that
349 will be passed as keywords arguments to the callable.
350
351 *description* can be provided to describe the format, and will be returned
352 by the :func:`get_unpack_formats` function.
353
354 .. versionadded:: 3.2
355
356
357.. function:: unregister_unpack_format(name)
358
359 Unregister an unpack format. *name* is the name of the format.
360
361 .. versionadded:: 3.2
362
363
364.. function:: get_unpack_formats()
365
366 Return a list of all registered formats for unpacking.
367 Each element of the returned sequence is a tuple
368 ``(name, extensions, description)``.
369
370 By default :mod:`shutil` provides these formats:
371
372 - *gztar*: gzip'ed tar-file
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000373 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000374 - *tar*: uncompressed tar file
375 - *zip*: ZIP file
376
377 You can register new formats or provide your own unpacker for any existing
378 formats, by using :func:`register_unpack_format`.
379
380 .. versionadded:: 3.2
381
382
Éric Araujof2fbb9c2012-01-16 16:55:55 +0100383.. _shutil-archiving-example:
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000384
Tarek Ziadé396fad72010-02-23 05:30:31 +0000385Archiving example
386:::::::::::::::::
387
388In this example, we create a gzip'ed tar-file archive containing all files
389found in the :file:`.ssh` directory of the user::
390
391 >>> from shutil import make_archive
392 >>> import os
393 >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
394 >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
395 >>> make_archive(archive_name, 'gztar', root_dir)
396 '/Users/tarek/myarchive.tar.gz'
397
398The resulting archive contains::
399
400 $ tar -tzvf /Users/tarek/myarchive.tar.gz
401 drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
402 -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
403 -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
404 -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
405 -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
406 -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
407 -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
408 -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts