blob: 53e34d75d7e28d1efc35c5e6f712e34f8e285eae [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
Guido van Rossumda27fd22007-08-17 00:24:54 +000018.. warning::
Christian Heimes7f044312008-01-06 17:05:40 +000019
20 Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
21 can't copy all file metadata.
Georg Brandl48310cd2009-01-03 21:18:54 +000022
Christian Heimes7f044312008-01-06 17:05:40 +000023 On POSIX platforms, this means that file owner and group are lost as well
Georg Brandlc575c902008-09-13 17:46:05 +000024 as ACLs. On Mac OS, the resource fork and other metadata are not used.
Christian Heimes7f044312008-01-06 17:05:40 +000025 This means that resources will be lost and file type and creator codes will
26 not be correct. On Windows, file owners, ACLs and alternate data streams
27 are not copied.
Georg Brandl116aa622007-08-15 14:28:22 +000028
Tarek Ziadé396fad72010-02-23 05:30:31 +000029Directory and files operations
30------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000031
Georg Brandl116aa622007-08-15 14:28:22 +000032.. function:: copyfileobj(fsrc, fdst[, length])
33
34 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
35 The integer *length*, if given, is the buffer size. In particular, a negative
36 *length* value means to copy the data without looping over the source data in
37 chunks; by default the data is read in chunks to avoid uncontrolled memory
38 consumption. Note that if the current file position of the *fsrc* object is not
39 0, only the contents from the current file position to the end of the file will
40 be copied.
41
42
Christian Heimesa342c012008-04-20 21:01:16 +000043.. function:: copyfile(src, dst)
44
45 Copy the contents (no metadata) of the file named *src* to a file named *dst*.
46 *dst* must be the complete target file name; look at :func:`copy` for a copy that
Georg Brandlaf265f42008-12-07 15:06:20 +000047 accepts a target directory path. If *src* and *dst* are the same files,
48 :exc:`Error` is raised.
Christian Heimesa342c012008-04-20 21:01:16 +000049 The destination location must be writable; otherwise, an :exc:`IOError` exception
50 will be raised. If *dst* already exists, it will be replaced. Special files
51 such as character or block devices and pipes cannot be copied with this
52 function. *src* and *dst* are path names given as strings.
53
54
Georg Brandl116aa622007-08-15 14:28:22 +000055.. function:: copymode(src, dst)
56
57 Copy the permission bits from *src* to *dst*. The file contents, owner, and
58 group are unaffected. *src* and *dst* are path names given as strings.
59
60
61.. function:: copystat(src, dst)
62
63 Copy the permission bits, last access time, last modification time, and flags
64 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
65 and *dst* are path names given as strings.
66
67
68.. function:: copy(src, dst)
69
70 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
71 file with the same basename as *src* is created (or overwritten) in the
72 directory specified. Permission bits are copied. *src* and *dst* are path
73 names given as strings.
74
75
76.. function:: copy2(src, dst)
77
Alexandre Vassalottibee32532008-05-16 18:15:12 +000078 Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just
79 :func:`copy` followed by :func:`copystat`. This is similar to the
80 Unix command :program:`cp -p`.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
Georg Brandl86b2fb92008-07-16 03:43:04 +000083.. function:: ignore_patterns(\*patterns)
84
85 This factory function creates a function that can be used as a callable for
86 :func:`copytree`\'s *ignore* argument, ignoring files and directories that
87 match one of the glob-style *patterns* provided. See the example below.
88
89
Tarek Ziadé5340db32010-04-19 22:30:51 +000090.. function:: copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2)
Georg Brandl116aa622007-08-15 14:28:22 +000091
92 Recursively copy an entire directory tree rooted at *src*. The destination
Georg Brandl86b2fb92008-07-16 03:43:04 +000093 directory, named by *dst*, must not already exist; it will be created as well
94 as missing parent directories. Permissions and times of directories are
95 copied with :func:`copystat`, individual files are copied using
96 :func:`copy2`.
Georg Brandl116aa622007-08-15 14:28:22 +000097
Georg Brandl86b2fb92008-07-16 03:43:04 +000098 If *symlinks* is true, symbolic links in the source tree are represented as
99 symbolic links in the new tree; if false or omitted, the contents of the
100 linked files are copied to the new tree.
101
Tarek Ziadéfb437512010-04-20 08:57:33 +0000102 When *symlinks* is false, if the file pointed by the symlink doesn't
103 exist, a exception will be added in the list of errors raised in
104 a :exc:`Error` exception at the end of the copy process.
105 You can set the optional *ignore_dangling_symlinks* flag to true if you
106 want to silent this exception.
107
Georg Brandl86b2fb92008-07-16 03:43:04 +0000108 If *ignore* is given, it must be a callable that will receive as its
109 arguments the directory being visited by :func:`copytree`, and a list of its
110 contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
111 called recursively, the *ignore* callable will be called once for each
112 directory that is copied. The callable must return a sequence of directory
113 and file names relative to the current directory (i.e. a subset of the items
114 in its second argument); these names will then be ignored in the copy
115 process. :func:`ignore_patterns` can be used to create such a callable that
116 ignores names based on glob-style patterns.
117
118 If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
119
Tarek Ziadé5340db32010-04-19 22:30:51 +0000120 If *copy_function* is given, it must be a callable that will be used
121 to copy each file. It will be called with the source path and the
122 destination path as arguments. By default, :func:`copy2` is used, but any
123 function that supports the same signature (like :func:`copy`) can be used.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Tarek Ziadé5340db32010-04-19 22:30:51 +0000125 .. versionchanged:: 3.2
126 Added the *copy_function* argument to be able to provide a custom copy
127 function.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
Tarek Ziadéfb437512010-04-20 08:57:33 +0000129 .. versionchanged:: 3.2
130 Added the *ignore_dangling_symlinks* argument to silent dangling symlinks
131 errors when *symlinks* is false.
132
133
Georg Brandl18244152009-09-02 20:34:52 +0000134.. function:: rmtree(path, ignore_errors=False, onerror=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136 .. index:: single: directory; deleting
137
Christian Heimes9bd667a2008-01-20 15:14:11 +0000138 Delete an entire directory tree; *path* must point to a directory (but not a
139 symbolic link to a directory). If *ignore_errors* is true, errors resulting
140 from failed removals will be ignored; if false or omitted, such errors are
141 handled by calling a handler specified by *onerror* or, if that is omitted,
142 they raise an exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Christian Heimes9bd667a2008-01-20 15:14:11 +0000144 If *onerror* is provided, it must be a callable that accepts three
145 parameters: *function*, *path*, and *excinfo*. The first parameter,
146 *function*, is the function which raised the exception; it will be
147 :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or
148 :func:`os.rmdir`. The second parameter, *path*, will be the path name passed
149 to *function*. The third parameter, *excinfo*, will be the exception
150 information return by :func:`sys.exc_info`. Exceptions raised by *onerror*
151 will not be caught.
152
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154.. function:: move(src, dst)
155
156 Recursively move a file or directory to another location.
157
Christian Heimes7f044312008-01-06 17:05:40 +0000158 If the destination is on the current filesystem, then simply use rename.
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000159 Otherwise, copy src (with :func:`copy2`) to the dst and then remove src.
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandl116aa622007-08-15 14:28:22 +0000161
162.. exception:: Error
163
Christian Heimes7f044312008-01-06 17:05:40 +0000164 This exception collects exceptions that raised during a multi-file operation. For
Georg Brandl116aa622007-08-15 14:28:22 +0000165 :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
166 *dstname*, *exception*).
167
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169.. _shutil-example:
170
Tarek Ziadé396fad72010-02-23 05:30:31 +0000171copytree example
172::::::::::::::::
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174This example is the implementation of the :func:`copytree` function, described
175above, with the docstring omitted. It demonstrates many of the other functions
176provided by this module. ::
177
178 def copytree(src, dst, symlinks=False):
179 names = os.listdir(src)
180 os.makedirs(dst)
181 errors = []
182 for name in names:
183 srcname = os.path.join(src, name)
184 dstname = os.path.join(dst, name)
185 try:
186 if symlinks and os.path.islink(srcname):
187 linkto = os.readlink(srcname)
188 os.symlink(linkto, dstname)
189 elif os.path.isdir(srcname):
190 copytree(srcname, dstname, symlinks)
191 else:
192 copy2(srcname, dstname)
193 # XXX What about devices, sockets etc.?
194 except (IOError, os.error) as why:
195 errors.append((srcname, dstname, str(why)))
196 # catch the Error from the recursive copytree so that we can
197 # continue with other files
198 except Error as err:
199 errors.extend(err.args[0])
200 try:
201 copystat(src, dst)
202 except WindowsError:
203 # can't copy file access times on Windows
204 pass
205 except OSError as why:
206 errors.extend((src, dst, str(why)))
207 if errors:
Collin Winterc79461b2007-09-01 23:34:30 +0000208 raise Error(errors)
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Tarek Ziadé396fad72010-02-23 05:30:31 +0000210Another example that uses the :func:`ignore_patterns` helper::
211
212 from shutil import copytree, ignore_patterns
213
214 copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
215
216This will copy everything except ``.pyc`` files and files or directories whose
217name starts with ``tmp``.
218
219Another example that uses the *ignore* argument to add a logging call::
220
221 from shutil import copytree
222 import logging
223
224 def _logpath(path, names):
225 logging.info('Working in %s' % path)
226 return [] # nothing will be ignored
227
228 copytree(source, destination, ignore=_logpath)
229
230
231Archives operations
232-------------------
233
234.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
235
236 Create an archive file (eg. zip or tar) and returns its name.
237
238 *base_name* is the name of the file to create, including the path, minus
239 any format-specific extension. *format* is the archive format: one of
Tarek Ziadé10a51af2010-04-19 21:31:42 +0000240 "zip", "tar", "ztar", "bztar" or "gztar".
Tarek Ziadé396fad72010-02-23 05:30:31 +0000241
242 *root_dir* is a directory that will be the root directory of the
243 archive; ie. we typically chdir into *root_dir* before creating the
244 archive.
245
246 *base_dir* is the directory where we start archiving from;
247 ie. *base_dir* will be the common prefix of all files and
248 directories in the archive.
249
250 *root_dir* and *base_dir* both default to the current directory.
251
252 *owner* and *group* are used when creating a tar archive. By default,
253 uses the current owner and group.
254
Ezio Melottif8754a62010-03-21 07:16:43 +0000255 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000256
257
258.. function:: get_archive_formats()
259
260 Returns a list of supported formats for archiving.
261 Each element of the returned sequence is a tuple ``(name, description)``
262
263 By default :mod:`shutil` provides these formats:
264
265 - *gztar*: gzip'ed tar-file
266 - *bztar*: bzip2'ed tar-file
267 - *ztar*: compressed tar file
268 - *tar*: uncompressed tar file
269 - *zip*: ZIP file
270
271 You can register new formats or provide your own archiver for any existing
272 formats, by using :func:`register_archive_format`.
273
Ezio Melottif8754a62010-03-21 07:16:43 +0000274 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000275
276
277.. function:: register_archive_format(name, function, [extra_args, [description]])
278
279 Registers an archiver for the format *name*. *function* is a callable that
280 will be used to invoke the archiver.
281
282 If given, *extra_args* is a sequence of ``(name, value)`` that will be
283 used as extra keywords arguments when the archiver callable is used.
284
285 *description* is used by :func:`get_archive_formats` which returns the
286 list of archivers. Defaults to an empty list.
287
Ezio Melottif8754a62010-03-21 07:16:43 +0000288 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000289
290
291.. function:: unregister_archive_format(name)
292
293 Remove the archive format *name* from the list of supported formats.
294
Ezio Melottif8754a62010-03-21 07:16:43 +0000295 .. versionadded:: 3.2
Tarek Ziadé396fad72010-02-23 05:30:31 +0000296
297
298Archiving example
299:::::::::::::::::
300
301In this example, we create a gzip'ed tar-file archive containing all files
302found in the :file:`.ssh` directory of the user::
303
304 >>> from shutil import make_archive
305 >>> import os
306 >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
307 >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
308 >>> make_archive(archive_name, 'gztar', root_dir)
309 '/Users/tarek/myarchive.tar.gz'
310
311The resulting archive contains::
312
313 $ tar -tzvf /Users/tarek/myarchive.tar.gz
314 drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
315 -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
316 -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
317 -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
318 -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
319 -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
320 -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
321 -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
322
323