blob: 1082d2a37b4e559c65aba893254f164add136cc5 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`shutil` --- High-level file operations
3============================================
4
5.. module:: shutil
6 :synopsis: High-level file operations, including copying.
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Georg Brandlb19be572007-12-29 10:57:00 +00008.. partly based on the docstrings
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
10.. index::
11 single: file; copying
12 single: copying files
13
14The :mod:`shutil` module offers a number of high-level operations on files and
15collections of files. In particular, functions are provided which support file
Mark Summerfieldac3d4292007-11-02 08:24:59 +000016copying and removal. For operations on individual files, see also the
17:mod:`os` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000018
Raymond Hettingere0e08222010-11-06 07:10:31 +000019.. seealso::
20
21 Latest version of the `shutil module Python source code
22 <http://svn.python.org/view/python/branches/release27-maint/Lib/shutil.py?view=markup>`_
23
Georg Brandlbf863b12007-08-15 19:06:04 +000024.. warning::
Georg Brandlec32b6b2008-01-06 16:12:39 +000025
26 Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
27 can't copy all file metadata.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000028
Georg Brandlec32b6b2008-01-06 16:12:39 +000029 On POSIX platforms, this means that file owner and group are lost as well
Georg Brandl9af94982008-09-13 17:41:16 +000030 as ACLs. On Mac OS, the resource fork and other metadata are not used.
Georg Brandlec32b6b2008-01-06 16:12:39 +000031 This means that resources will be lost and file type and creator codes will
32 not be correct. On Windows, file owners, ACLs and alternate data streams
33 are not copied.
Georg Brandl8ec7f652007-08-15 14:28:01 +000034
Raymond Hettingere0e08222010-11-06 07:10:31 +000035
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000036Directory and files operations
37------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000038
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl786ead62008-04-19 16:57:43 +000050.. function:: copyfile(src, dst)
51
52 Copy the contents (no metadata) of the file named *src* to a file named *dst*.
53 *dst* must be the complete target file name; look at :func:`copy` for a copy that
Georg Brandl905e0f62008-12-05 15:32:29 +000054 accepts a target directory path. If *src* and *dst* are the same files,
55 :exc:`Error` is raised.
Georg Brandl786ead62008-04-19 16:57:43 +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 Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl88107da2008-05-16 13:18:50 +000085 Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just
86 :func:`copy` followed by :func:`copystat`. This is similar to the
87 Unix command :program:`cp -p`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
89
Georg Brandle78fbcc2008-07-05 10:13:36 +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
Andrew M. Kuchlingc4060842008-07-06 17:43:16 +000094 match one of the glob-style *patterns* provided. See the example below.
Georg Brandle78fbcc2008-07-05 10:13:36 +000095
96 .. versionadded:: 2.6
97
98
99.. function:: copytree(src, dst[, symlinks=False[, ignore=None]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
101 Recursively copy an entire directory tree rooted at *src*. The destination
Georg Brandle78fbcc2008-07-05 10:13:36 +0000102 directory, named by *dst*, must not already exist; it will be created as well
103 as missing parent directories. Permissions and times of directories are
104 copied with :func:`copystat`, individual files are copied using
105 :func:`copy2`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106
Georg Brandle78fbcc2008-07-05 10:13:36 +0000107 If *symlinks* is true, symbolic links in the source tree are represented as
108 symbolic links in the new tree; if false or omitted, the contents of the
109 linked files are copied to the new tree.
110
111 If *ignore* is given, it must be a callable that will receive as its
112 arguments the directory being visited by :func:`copytree`, and a list of its
113 contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
114 called recursively, the *ignore* callable will be called once for each
115 directory that is copied. The callable must return a sequence of directory
116 and file names relative to the current directory (i.e. a subset of the items
117 in its second argument); these names will then be ignored in the copy
118 process. :func:`ignore_patterns` can be used to create such a callable that
119 ignores names based on glob-style patterns.
120
121 If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
122
123 The source code for this should be considered an example rather than the
124 ultimate tool.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125
126 .. versionchanged:: 2.3
127 :exc:`Error` is raised if any exceptions occur during copying, rather than
128 printing a message.
129
130 .. versionchanged:: 2.5
131 Create intermediate directories needed to create *dst*, rather than raising an
132 error. Copy permissions and times of directories using :func:`copystat`.
133
Georg Brandle78fbcc2008-07-05 10:13:36 +0000134 .. versionchanged:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000135 Added the *ignore* argument to be able to influence what is being copied.
Georg Brandle78fbcc2008-07-05 10:13:36 +0000136
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137
138.. function:: rmtree(path[, ignore_errors[, onerror]])
139
140 .. index:: single: directory; deleting
141
Georg Brandl52353982008-01-20 14:17:42 +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 Brandl8ec7f652007-08-15 14:28:01 +0000147
Georg Brandl52353982008-01-20 14:17:42 +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
157 .. versionchanged:: 2.6
158 Explicitly check for *path* being a symbolic link and raise :exc:`OSError`
159 in that case.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160
161
162.. function:: move(src, dst)
163
Éric Araujod01aebe2011-07-29 12:10:53 +0200164 Recursively move a file or directory (*src*) to another location (*dst*).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
Éric Araujod01aebe2011-07-29 12:10:53 +0200166 If the destination is a directory or a symlink to a directory, then *src* is
167 moved inside that directory.
168
169 The destination directory must not already exist. If the destination already
170 exists but is not a directory, it may be overwritten depending on
171 :func:`os.rename` semantics.
172
173 If the destination is on the current filesystem, then :func:`os.rename` is
174 used. Otherwise, *src* is copied (using :func:`copy2`) to *dst* and then
175 removed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000176
177 .. versionadded:: 2.3
178
179
180.. exception:: Error
181
Éric Araujod01aebe2011-07-29 12:10:53 +0200182 This exception collects exceptions that are raised during a multi-file
183 operation. For :func:`copytree`, the exception argument is a list of 3-tuples
184 (*srcname*, *dstname*, *exception*).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000185
186 .. versionadded:: 2.3
187
Éric Araujod01aebe2011-07-29 12:10:53 +0200188
Georg Brandl8ec7f652007-08-15 14:28:01 +0000189.. _shutil-example:
190
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000191copytree example
192::::::::::::::::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000193
194This example is the implementation of the :func:`copytree` function, described
195above, with the docstring omitted. It demonstrates many of the other functions
196provided by this module. ::
197
Georg Brandle78fbcc2008-07-05 10:13:36 +0000198 def copytree(src, dst, symlinks=False, ignore=None):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000199 names = os.listdir(src)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000200 if ignore is not None:
201 ignored_names = ignore(src, names)
202 else:
203 ignored_names = set()
204
Georg Brandl8ec7f652007-08-15 14:28:01 +0000205 os.makedirs(dst)
206 errors = []
207 for name in names:
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000208 if name in ignored_names:
Georg Brandle78fbcc2008-07-05 10:13:36 +0000209 continue
Georg Brandl8ec7f652007-08-15 14:28:01 +0000210 srcname = os.path.join(src, name)
211 dstname = os.path.join(dst, name)
212 try:
213 if symlinks and os.path.islink(srcname):
214 linkto = os.readlink(srcname)
215 os.symlink(linkto, dstname)
216 elif os.path.isdir(srcname):
Georg Brandle78fbcc2008-07-05 10:13:36 +0000217 copytree(srcname, dstname, symlinks, ignore)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000218 else:
219 copy2(srcname, dstname)
220 # XXX What about devices, sockets etc.?
221 except (IOError, os.error), why:
222 errors.append((srcname, dstname, str(why)))
223 # catch the Error from the recursive copytree so that we can
224 # continue with other files
225 except Error, err:
226 errors.extend(err.args[0])
227 try:
228 copystat(src, dst)
229 except WindowsError:
230 # can't copy file access times on Windows
231 pass
232 except OSError, why:
233 errors.extend((src, dst, str(why)))
234 if errors:
Georg Brandlc1edec32009-06-03 07:25:35 +0000235 raise Error(errors)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000236
237Another example that uses the :func:`ignore_patterns` helper::
238
239 from shutil import copytree, ignore_patterns
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000240
Georg Brandle78fbcc2008-07-05 10:13:36 +0000241 copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
242
243This will copy everything except ``.pyc`` files and files or directories whose
244name starts with ``tmp``.
245
246Another example that uses the *ignore* argument to add a logging call::
247
248 from shutil import copytree
249 import logging
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000250
Georg Brandle78fbcc2008-07-05 10:13:36 +0000251 def _logpath(path, names):
252 logging.info('Working in %s' % path)
253 return [] # nothing will be ignored
254
255 copytree(source, destination, ignore=_logpath)
256
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000257
258Archives operations
259-------------------
260
261.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
262
263 Create an archive file (eg. zip or tar) and returns its name.
264
265 *base_name* is the name of the file to create, including the path, minus
266 any format-specific extension. *format* is the archive format: one of
Tarek Ziadée593fad2010-04-20 21:09:06 +0000267 "zip", "tar", "bztar" or "gztar".
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000268
269 *root_dir* is a directory that will be the root directory of the
270 archive; ie. we typically chdir into *root_dir* before creating the
271 archive.
272
273 *base_dir* is the directory where we start archiving from;
274 ie. *base_dir* will be the common prefix of all files and
275 directories in the archive.
276
277 *root_dir* and *base_dir* both default to the current directory.
278
279 *owner* and *group* are used when creating a tar archive. By default,
280 uses the current owner and group.
281
282 .. versionadded:: 2.7
283
284
285.. function:: get_archive_formats()
286
Éric Araujod01aebe2011-07-29 12:10:53 +0200287 Return a list of supported formats for archiving.
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000288 Each element of the returned sequence is a tuple ``(name, description)``
289
290 By default :mod:`shutil` provides these formats:
291
292 - *gztar*: gzip'ed tar-file
293 - *bztar*: bzip2'ed tar-file
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000294 - *tar*: uncompressed tar file
295 - *zip*: ZIP file
296
297 You can register new formats or provide your own archiver for any existing
298 formats, by using :func:`register_archive_format`.
299
300 .. versionadded:: 2.7
301
302
303.. function:: register_archive_format(name, function, [extra_args, [description]])
304
Éric Araujod01aebe2011-07-29 12:10:53 +0200305 Register an archiver for the format *name*. *function* is a callable that
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000306 will be used to invoke the archiver.
307
308 If given, *extra_args* is a sequence of ``(name, value)`` that will be
309 used as extra keywords arguments when the archiver callable is used.
310
311 *description* is used by :func:`get_archive_formats` which returns the
312 list of archivers. Defaults to an empty list.
313
314 .. versionadded:: 2.7
315
316
317.. function:: unregister_archive_format(name)
318
319 Remove the archive format *name* from the list of supported formats.
320
321 .. versionadded:: 2.7
322
323
324Archiving example
325:::::::::::::::::
326
327In this example, we create a gzip'ed tar-file archive containing all files
328found in the :file:`.ssh` directory of the user::
329
330 >>> from shutil import make_archive
331 >>> import os
332 >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
333 >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
334 >>> make_archive(archive_name, 'gztar', root_dir)
335 '/Users/tarek/myarchive.tar.gz'
336
337The resulting archive contains::
338
339 $ tar -tzvf /Users/tarek/myarchive.tar.gz
340 drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
341 -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
342 -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
343 -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
344 -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
345 -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
346 -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
347 -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
348
349