blob: 01448767194a8d6c863d21cc94cad360068d6923 [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
13The :mod:`shutil` module offers a number of high-level operations on files and
14collections of files. In particular, functions are provided which support file
Guido van Rossum2cc30da2007-11-02 23:46:40 +000015copying and removal. For operations on individual files, see also the
16:mod:`os` module.
Georg Brandl116aa622007-08-15 14:28:22 +000017
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000018.. seealso::
19
20 Latest version of the :source:`shutil module Python source code
21 <Lib/shutil.py>`
22
Guido van Rossumda27fd22007-08-17 00:24:54 +000023.. warning::
Christian Heimes7f044312008-01-06 17:05:40 +000024
25 Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
26 can't copy all file metadata.
Georg Brandl48310cd2009-01-03 21:18:54 +000027
Christian Heimes7f044312008-01-06 17:05:40 +000028 On POSIX platforms, this means that file owner and group are lost as well
Georg Brandlc575c902008-09-13 17:46:05 +000029 as ACLs. On Mac OS, the resource fork and other metadata are not used.
Christian Heimes7f044312008-01-06 17:05:40 +000030 This means that resources will be lost and file type and creator codes will
31 not be correct. On Windows, file owners, ACLs and alternate data streams
32 are not copied.
Georg Brandl116aa622007-08-15 14:28:22 +000033
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000034
Tarek Ziadé396fad72010-02-23 05:30:31 +000035Directory and files operations
36------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000037
Georg Brandl116aa622007-08-15 14:28:22 +000038.. function:: copyfileobj(fsrc, fdst[, length])
39
40 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
41 The integer *length*, if given, is the buffer size. In particular, a negative
42 *length* value means to copy the data without looping over the source data in
43 chunks; by default the data is read in chunks to avoid uncontrolled memory
44 consumption. Note that if the current file position of the *fsrc* object is not
45 0, only the contents from the current file position to the end of the file will
46 be copied.
47
48
Christian Heimesa342c012008-04-20 21:01:16 +000049.. function:: copyfile(src, dst)
50
51 Copy the contents (no metadata) of the file named *src* to a file named *dst*.
52 *dst* must be the complete target file name; look at :func:`copy` for a copy that
Georg Brandlaf265f42008-12-07 15:06:20 +000053 accepts a target directory path. If *src* and *dst* are the same files,
54 :exc:`Error` is raised.
Christian Heimesa342c012008-04-20 21:01:16 +000055 The destination location must be writable; otherwise, an :exc:`IOError` exception
56 will be raised. If *dst* already exists, it will be replaced. Special files
57 such as character or block devices and pipes cannot be copied with this
58 function. *src* and *dst* are path names given as strings.
59
60
Georg Brandl116aa622007-08-15 14:28:22 +000061.. function:: copymode(src, dst)
62
63 Copy the permission bits from *src* to *dst*. The file contents, owner, and
64 group are unaffected. *src* and *dst* are path names given as strings.
65
66
67.. function:: copystat(src, dst)
68
69 Copy the permission bits, last access time, last modification time, and flags
70 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
71 and *dst* are path names given as strings.
72
73
74.. function:: copy(src, dst)
75
76 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
77 file with the same basename as *src* is created (or overwritten) in the
78 directory specified. Permission bits are copied. *src* and *dst* are path
79 names given as strings.
80
81
82.. function:: copy2(src, dst)
83
Alexandre Vassalottibee32532008-05-16 18:15:12 +000084 Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just
85 :func:`copy` followed by :func:`copystat`. This is similar to the
86 Unix command :program:`cp -p`.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
Georg Brandl86b2fb92008-07-16 03:43:04 +000089.. function:: ignore_patterns(\*patterns)
90
91 This factory function creates a function that can be used as a callable for
92 :func:`copytree`\'s *ignore* argument, ignoring files and directories that
93 match one of the glob-style *patterns* provided. See the example below.
94
95
Ezio Melotticb999a32010-04-20 11:26:51 +000096.. function:: copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +000097
98 Recursively copy an entire directory tree rooted at *src*. The destination
Georg Brandl86b2fb92008-07-16 03:43:04 +000099 directory, named by *dst*, must not already exist; it will be created as well
100 as missing parent directories. Permissions and times of directories are
101 copied with :func:`copystat`, individual files are copied using
102 :func:`copy2`.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Georg Brandl86b2fb92008-07-16 03:43:04 +0000104 If *symlinks* is true, symbolic links in the source tree are represented as
105 symbolic links in the new tree; if false or omitted, the contents of the
106 linked files are copied to the new tree.
107
Tarek Ziadéfb437512010-04-20 08:57:33 +0000108 When *symlinks* is false, if the file pointed by the symlink doesn't
109 exist, a exception will be added in the list of errors raised in
110 a :exc:`Error` exception at the end of the copy process.
111 You can set the optional *ignore_dangling_symlinks* flag to true if you
Tarek Ziadé8c26c7d2010-04-23 13:03:50 +0000112 want to silence this exception. Notice that this option has no effect
113 on platforms that don't support :func:`os.symlink`.
Tarek Ziadéfb437512010-04-20 08:57:33 +0000114
Georg Brandl86b2fb92008-07-16 03:43:04 +0000115 If *ignore* is given, it must be a callable that will receive as its
116 arguments the directory being visited by :func:`copytree`, and a list of its
117 contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
118 called recursively, the *ignore* callable will be called once for each
119 directory that is copied. The callable must return a sequence of directory
120 and file names relative to the current directory (i.e. a subset of the items
121 in its second argument); these names will then be ignored in the copy
122 process. :func:`ignore_patterns` can be used to create such a callable that
123 ignores names based on glob-style patterns.
124
125 If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
126
Tarek Ziadé5340db32010-04-19 22:30:51 +0000127 If *copy_function* is given, it must be a callable that will be used
128 to copy each file. It will be called with the source path and the
129 destination path as arguments. By default, :func:`copy2` is used, but any
130 function that supports the same signature (like :func:`copy`) can be used.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
Tarek Ziadé5340db32010-04-19 22:30:51 +0000132 .. versionchanged:: 3.2
133 Added the *copy_function* argument to be able to provide a custom copy
134 function.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Ezio Melotticb999a32010-04-20 11:26:51 +0000136 .. versionchanged:: 3.2
Tarek Ziadéfb437512010-04-20 08:57:33 +0000137 Added the *ignore_dangling_symlinks* argument to silent dangling symlinks
138 errors when *symlinks* is false.
139
140
Georg Brandl18244152009-09-02 20:34:52 +0000141.. function:: rmtree(path, ignore_errors=False, onerror=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143 .. index:: single: directory; deleting
144
Christian Heimes9bd667a2008-01-20 15:14:11 +0000145 Delete an entire directory tree; *path* must point to a directory (but not a
146 symbolic link to a directory). If *ignore_errors* is true, errors resulting
147 from failed removals will be ignored; if false or omitted, such errors are
148 handled by calling a handler specified by *onerror* or, if that is omitted,
149 they raise an exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Christian Heimes9bd667a2008-01-20 15:14:11 +0000151 If *onerror* is provided, it must be a callable that accepts three
152 parameters: *function*, *path*, and *excinfo*. The first parameter,
153 *function*, is the function which raised the exception; it will be
154 :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or
155 :func:`os.rmdir`. The second parameter, *path*, will be the path name passed
156 to *function*. The third parameter, *excinfo*, will be the exception
157 information return by :func:`sys.exc_info`. Exceptions raised by *onerror*
158 will not be caught.
159
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161.. function:: move(src, dst)
162
163 Recursively move a file or directory to another location.
164
Christian Heimes7f044312008-01-06 17:05:40 +0000165 If the destination is on the current filesystem, then simply use rename.
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000166 Otherwise, copy src (with :func:`copy2`) to the dst and then remove src.
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169.. exception:: Error
170
Christian Heimes7f044312008-01-06 17:05:40 +0000171 This exception collects exceptions that raised during a multi-file operation. For
Georg Brandl116aa622007-08-15 14:28:22 +0000172 :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
173 *dstname*, *exception*).
174
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176.. _shutil-example:
177
Tarek Ziadé396fad72010-02-23 05:30:31 +0000178copytree example
179::::::::::::::::
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181This example is the implementation of the :func:`copytree` function, described
182above, with the docstring omitted. It demonstrates many of the other functions
183provided by this module. ::
184
185 def copytree(src, dst, symlinks=False):
186 names = os.listdir(src)
187 os.makedirs(dst)
188 errors = []
189 for name in names:
190 srcname = os.path.join(src, name)
191 dstname = os.path.join(dst, name)
192 try:
193 if symlinks and os.path.islink(srcname):
194 linkto = os.readlink(srcname)
195 os.symlink(linkto, dstname)
196 elif os.path.isdir(srcname):
197 copytree(srcname, dstname, symlinks)
198 else:
199 copy2(srcname, dstname)
200 # XXX What about devices, sockets etc.?
201 except (IOError, os.error) as why:
202 errors.append((srcname, dstname, str(why)))
203 # catch the Error from the recursive copytree so that we can
204 # continue with other files
205 except Error as err:
206 errors.extend(err.args[0])
207 try:
208 copystat(src, dst)
209 except WindowsError:
210 # can't copy file access times on Windows
211 pass
212 except OSError as why:
213 errors.extend((src, dst, str(why)))
214 if errors:
Collin Winterc79461b2007-09-01 23:34:30 +0000215 raise Error(errors)
Georg Brandl116aa622007-08-15 14:28:22 +0000216
Tarek Ziadé396fad72010-02-23 05:30:31 +0000217Another example that uses the :func:`ignore_patterns` helper::
218
219 from shutil import copytree, ignore_patterns
220
221 copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
222
223This will copy everything except ``.pyc`` files and files or directories whose
224name starts with ``tmp``.
225
226Another example that uses the *ignore* argument to add a logging call::
227
228 from shutil import copytree
229 import logging
230
231 def _logpath(path, names):
232 logging.info('Working in %s' % path)
233 return [] # nothing will be ignored
234
235 copytree(source, destination, ignore=_logpath)
236
237
238Archives operations
239-------------------
240
241.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
242
Ezio Melotticb999a32010-04-20 11:26:51 +0000243 Create an archive file (e.g. zip or tar) and returns its name.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000244
245 *base_name* is the name of the file to create, including the path, minus
246 any format-specific extension. *format* is the archive format: one of
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000247 "zip", "tar", "bztar" (if the :mod:`bz2` module is available) or "gztar".
Tarek Ziadé396fad72010-02-23 05:30:31 +0000248
249 *root_dir* is a directory that will be the root directory of the
Ezio Melotticb999a32010-04-20 11:26:51 +0000250 archive; i.e. we typically chdir into *root_dir* before creating the
Tarek Ziadé396fad72010-02-23 05:30:31 +0000251 archive.
252
253 *base_dir* is the directory where we start archiving from;
Ezio Melotticb999a32010-04-20 11:26:51 +0000254 i.e. *base_dir* will be the common prefix of all files and
Tarek Ziadé396fad72010-02-23 05:30:31 +0000255 directories in the archive.
256
257 *root_dir* and *base_dir* both default to the current directory.
258
259 *owner* and *group* are used when creating a tar archive. By default,
260 uses the current owner and group.
261
Ezio Melottif8754a62010-03-21 07:16:43 +0000262 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000263
264
265.. function:: get_archive_formats()
266
267 Returns a list of supported formats for archiving.
268 Each element of the returned sequence is a tuple ``(name, description)``
269
270 By default :mod:`shutil` provides these formats:
271
272 - *gztar*: gzip'ed tar-file
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000273 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000274 - *tar*: uncompressed tar file
275 - *zip*: ZIP file
276
277 You can register new formats or provide your own archiver for any existing
278 formats, by using :func:`register_archive_format`.
279
Ezio Melottif8754a62010-03-21 07:16:43 +0000280 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000281
282
283.. function:: register_archive_format(name, function, [extra_args, [description]])
284
285 Registers an archiver for the format *name*. *function* is a callable that
286 will be used to invoke the archiver.
287
288 If given, *extra_args* is a sequence of ``(name, value)`` that will be
289 used as extra keywords arguments when the archiver callable is used.
290
291 *description* is used by :func:`get_archive_formats` which returns the
292 list of archivers. Defaults to an empty list.
293
Ezio Melottif8754a62010-03-21 07:16:43 +0000294 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000295
296
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000297.. function:: unregister_archive_format(name)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000298
299 Remove the archive format *name* from the list of supported formats.
300
Ezio Melottif8754a62010-03-21 07:16:43 +0000301 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000302
303
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000304.. function:: unpack_archive(filename[, extract_dir[, format]])
305
306 Unpack an archive. *filename* is the full path of the archive.
307
308 *extract_dir* is the name of the target directory where the archive is
309 unpacked. If not provided, the current working directory is used.
310
311 *format* is the archive format: one of "zip", "tar", or "gztar". Or any
312 other format registered with :func:`register_unpack_format`. If not
313 provided, :func:`unpack_archive` will use the archive file name extension
314 and see if an unpacker was registered for that extension. In case none is
315 found, a :exc:`ValueError` is raised.
316
317 .. versionadded:: 3.2
318
319
320.. function:: register_unpack_format(name, extensions, function[, extra_args[,description]])
321
322 Registers an unpack format. *name* is the name of the format and
323 *extensions* is a list of extensions corresponding to the format, like
324 ``.zip`` for Zip files.
325
326 *function* is the callable that will be used to unpack archives. The
327 callable will receive the path of the archive, followed by the directory
328 the archive must be extracted to.
329
330 When provided, *extra_args* is a sequence of ``(name, value)`` tuples that
331 will be passed as keywords arguments to the callable.
332
333 *description* can be provided to describe the format, and will be returned
334 by the :func:`get_unpack_formats` function.
335
336 .. versionadded:: 3.2
337
338
339.. function:: unregister_unpack_format(name)
340
341 Unregister an unpack format. *name* is the name of the format.
342
343 .. versionadded:: 3.2
344
345
346.. function:: get_unpack_formats()
347
348 Return a list of all registered formats for unpacking.
349 Each element of the returned sequence is a tuple
350 ``(name, extensions, description)``.
351
352 By default :mod:`shutil` provides these formats:
353
354 - *gztar*: gzip'ed tar-file
Tarek Ziadéffa155a2010-04-29 13:34:35 +0000355 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000356 - *tar*: uncompressed tar file
357 - *zip*: ZIP file
358
359 You can register new formats or provide your own unpacker for any existing
360 formats, by using :func:`register_unpack_format`.
361
362 .. versionadded:: 3.2
363
364
365
Tarek Ziadé396fad72010-02-23 05:30:31 +0000366Archiving example
367:::::::::::::::::
368
369In this example, we create a gzip'ed tar-file archive containing all files
370found in the :file:`.ssh` directory of the user::
371
372 >>> from shutil import make_archive
373 >>> import os
374 >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
375 >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
376 >>> make_archive(archive_name, 'gztar', root_dir)
377 '/Users/tarek/myarchive.tar.gz'
378
379The resulting archive contains::
380
381 $ tar -tzvf /Users/tarek/myarchive.tar.gz
382 drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
383 -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
384 -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
385 -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
386 -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
387 -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
388 -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
389 -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
390
391