blob: d8826277a46c29842b10820fc4233d4612397e05 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00008.. partly based on the docstrings
Georg Brandl116aa622007-08-15 14:28:22 +00009
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/shutil.py`
11
Georg Brandl116aa622007-08-15 14:28:22 +000012.. index::
13 single: file; copying
14 single: copying files
15
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000016--------------
17
Georg Brandl116aa622007-08-15 14:28:22 +000018The :mod:`shutil` module offers a number of high-level operations on files and
19collections of files. In particular, functions are provided which support file
Guido van Rossum2cc30da2007-11-02 23:46:40 +000020copying and removal. For operations on individual files, see also the
21:mod:`os` module.
Georg Brandl116aa622007-08-15 14:28:22 +000022
Guido van Rossumda27fd22007-08-17 00:24:54 +000023.. warning::
Christian Heimes7f044312008-01-06 17:05:40 +000024
Senthil Kumaran7f728c12012-02-13 23:30:47 +080025 Even the higher-level file copying functions (:func:`shutil.copy`,
26 :func:`shutil.copy2`) cannot 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
Éric Araujof2fbb9c2012-01-16 16:55:55 +010035.. _file-operations:
36
Tarek Ziadé396fad72010-02-23 05:30:31 +000037Directory and files operations
38------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000039
Georg Brandl116aa622007-08-15 14:28:22 +000040.. function:: copyfileobj(fsrc, fdst[, length])
41
42 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
43 The integer *length*, if given, is the buffer size. In particular, a negative
44 *length* value means to copy the data without looping over the source data in
45 chunks; by default the data is read in chunks to avoid uncontrolled memory
46 consumption. Note that if the current file position of the *fsrc* object is not
47 0, only the contents from the current file position to the end of the file will
48 be copied.
49
50
Larry Hastingsb4038062012-07-15 10:57:38 -070051.. function:: copyfile(src, dst, *, follow_symlinks=True)
Christian Heimesa342c012008-04-20 21:01:16 +000052
Senthil Kumaran7f728c12012-02-13 23:30:47 +080053 Copy the contents (no metadata) of the file named *src* to a file named
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020054 *dst* and return *dst* in the most efficient way possible.
55 *src* and *dst* are path names given as strings.
56
Larry Hastings60eba572012-09-21 10:12:14 -070057 *dst* must be the complete target file name; look at :func:`shutil.copy`
58 for a copy that accepts a target directory path. If *src* and *dst*
Hynek Schlawack48653762012-10-07 12:49:58 +020059 specify the same file, :exc:`SameFileError` is raised.
Senthil Kumaran1fd64822012-02-13 23:35:44 +080060
Larry Hastings60eba572012-09-21 10:12:14 -070061 The destination location must be writable; otherwise, an :exc:`OSError`
62 exception will be raised. If *dst* already exists, it will be replaced.
63 Special files such as character or block devices and pipes cannot be
64 copied with this function.
Christian Heimesa342c012008-04-20 21:01:16 +000065
Larry Hastings7aa2c8b2012-07-15 16:58:29 -070066 If *follow_symlinks* is false and *src* is a symbolic link,
67 a new symbolic link will be created instead of copying the
68 file *src* points to.
Antoine Pitrou78091e62011-12-29 18:54:15 +010069
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020070 .. versionchanged:: 3.3
71 :exc:`IOError` used to be raised instead of :exc:`OSError`.
Larry Hastings7aa2c8b2012-07-15 16:58:29 -070072 Added *follow_symlinks* argument.
73 Now returns *dst*.
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020074
Hynek Schlawack48653762012-10-07 12:49:58 +020075 .. versionchanged:: 3.4
Hynek Schlawack27ddb572012-10-28 13:59:27 +010076 Raise :exc:`SameFileError` instead of :exc:`Error`. Since the former is
77 a subclass of the latter, this change is backward compatible.
Hynek Schlawack48653762012-10-07 12:49:58 +020078
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020079 .. versionchanged:: 3.8
80 Platform-specific fast-copy syscalls may be used internally in order to
81 copy the file more efficiently. See
82 :ref:`shutil-platform-dependent-efficient-copy-operations` section.
Hynek Schlawack48653762012-10-07 12:49:58 +020083
84.. exception:: SameFileError
85
86 This exception is raised if source and destination in :func:`copyfile`
87 are the same file.
88
89 .. versionadded:: 3.4
90
91
Larry Hastings7aa2c8b2012-07-15 16:58:29 -070092.. function:: copymode(src, dst, *, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +000093
94 Copy the permission bits from *src* to *dst*. The file contents, owner, and
Larry Hastings60eba572012-09-21 10:12:14 -070095 group are unaffected. *src* and *dst* are path names given as strings.
96 If *follow_symlinks* is false, and both *src* and *dst* are symbolic links,
97 :func:`copymode` will attempt to modify the mode of *dst* itself (rather
98 than the file it points to). This functionality is not available on every
99 platform; please see :func:`copystat` for more information. If
100 :func:`copymode` cannot modify symbolic links on the local platform, and it
101 is asked to do so, it will do nothing and return.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
Antoine Pitrou78091e62011-12-29 18:54:15 +0100103 .. versionchanged:: 3.3
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700104 Added *follow_symlinks* argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700106.. function:: copystat(src, dst, *, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Larry Hastings60eba572012-09-21 10:12:14 -0700108 Copy the permission bits, last access time, last modification time, and
109 flags from *src* to *dst*. On Linux, :func:`copystat` also copies the
110 "extended attributes" where possible. The file contents, owner, and
111 group are unaffected. *src* and *dst* are path names given as strings.
112
113 If *follow_symlinks* is false, and *src* and *dst* both
114 refer to symbolic links, :func:`copystat` will operate on
115 the symbolic links themselves rather than the files the
Martin Panter357ed2e2016-11-21 00:15:20 +0000116 symbolic links refer to—reading the information from the
Larry Hastings60eba572012-09-21 10:12:14 -0700117 *src* symbolic link, and writing the information to the
118 *dst* symbolic link.
119
120 .. note::
121
122 Not all platforms provide the ability to examine and
123 modify symbolic links. Python itself can tell you what
124 functionality is locally available.
125
126 * If ``os.chmod in os.supports_follow_symlinks`` is
127 ``True``, :func:`copystat` can modify the permission
128 bits of a symbolic link.
129
130 * If ``os.utime in os.supports_follow_symlinks`` is
131 ``True``, :func:`copystat` can modify the last access
132 and modification times of a symbolic link.
133
134 * If ``os.chflags in os.supports_follow_symlinks`` is
135 ``True``, :func:`copystat` can modify the flags of
136 a symbolic link. (``os.chflags`` is not available on
137 all platforms.)
138
139 On platforms where some or all of this functionality
140 is unavailable, when asked to modify a symbolic link,
141 :func:`copystat` will copy everything it can.
142 :func:`copystat` never returns failure.
143
144 Please see :data:`os.supports_follow_symlinks`
145 for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Antoine Pitrou78091e62011-12-29 18:54:15 +0100147 .. versionchanged:: 3.3
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700148 Added *follow_symlinks* argument and support for Linux extended attributes.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700150.. function:: copy(src, dst, *, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Larry Hastings60eba572012-09-21 10:12:14 -0700152 Copies the file *src* to the file or directory *dst*. *src* and *dst*
153 should be strings. If *dst* specifies a directory, the file will be
154 copied into *dst* using the base filename from *src*. Returns the
155 path to the newly created file.
156
157 If *follow_symlinks* is false, and *src* is a symbolic link,
158 *dst* will be created as a symbolic link. If *follow_symlinks*
159 is true and *src* is a symbolic link, *dst* will be a copy of
160 the file *src* refers to.
161
Mariatta70ee0cd2017-03-10 18:17:21 -0800162 :func:`~shutil.copy` copies the file data and the file's permission
Larry Hastings60eba572012-09-21 10:12:14 -0700163 mode (see :func:`os.chmod`). Other metadata, like the
164 file's creation and modification times, is not preserved.
165 To preserve all file metadata from the original, use
166 :func:`~shutil.copy2` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Antoine Pitrou78091e62011-12-29 18:54:15 +0100168 .. versionchanged:: 3.3
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700169 Added *follow_symlinks* argument.
Larry Hastings60eba572012-09-21 10:12:14 -0700170 Now returns path to the newly created file.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200172 .. versionchanged:: 3.8
173 Platform-specific fast-copy syscalls may be used internally in order to
174 copy the file more efficiently. See
175 :ref:`shutil-platform-dependent-efficient-copy-operations` section.
176
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700177.. function:: copy2(src, dst, *, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Larry Hastings60eba572012-09-21 10:12:14 -0700179 Identical to :func:`~shutil.copy` except that :func:`copy2`
180 also attempts to preserve all file metadata.
181
182 When *follow_symlinks* is false, and *src* is a symbolic
183 link, :func:`copy2` attempts to copy all metadata from the
184 *src* symbolic link to the newly-created *dst* symbolic link.
185 However, this functionality is not available on all platforms.
186 On platforms where some or all of this functionality is
187 unavailable, :func:`copy2` will preserve all the metadata
188 it can; :func:`copy2` never returns failure.
189
190 :func:`copy2` uses :func:`copystat` to copy the file metadata.
191 Please see :func:`copystat` for more information
192 about platform support for modifying symbolic link metadata.
Georg Brandl116aa622007-08-15 14:28:22 +0000193
Antoine Pitrou78091e62011-12-29 18:54:15 +0100194 .. versionchanged:: 3.3
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700195 Added *follow_symlinks* argument, try to copy extended
196 file system attributes too (currently Linux only).
Larry Hastings60eba572012-09-21 10:12:14 -0700197 Now returns path to the newly created file.
Brian Curtin066dacf2012-06-19 10:03:05 -0500198
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200199 .. versionchanged:: 3.8
200 Platform-specific fast-copy syscalls may be used internally in order to
201 copy the file more efficiently. See
202 :ref:`shutil-platform-dependent-efficient-copy-operations` section.
203
Georg Brandl86b2fb92008-07-16 03:43:04 +0000204.. function:: ignore_patterns(\*patterns)
205
206 This factory function creates a function that can be used as a callable for
207 :func:`copytree`\'s *ignore* argument, ignoring files and directories that
208 match one of the glob-style *patterns* provided. See the example below.
209
210
R David Murray6ffface2014-06-11 14:40:13 -0400211.. function:: copytree(src, dst, symlinks=False, ignore=None, \
212 copy_function=copy2, ignore_dangling_symlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Brian Curtin0d0a1de2012-06-18 18:41:07 -0500214 Recursively copy an entire directory tree rooted at *src*, returning the
215 destination directory. The destination
Senthil Kumaran7f728c12012-02-13 23:30:47 +0800216 directory, named by *dst*, must not already exist; it will be created as
217 well as missing parent directories. Permissions and times of directories
218 are copied with :func:`copystat`, individual files are copied using
219 :func:`shutil.copy2`.
Georg Brandl116aa622007-08-15 14:28:22 +0000220
Georg Brandl86b2fb92008-07-16 03:43:04 +0000221 If *symlinks* is true, symbolic links in the source tree are represented as
Antoine Pitrou78091e62011-12-29 18:54:15 +0100222 symbolic links in the new tree and the metadata of the original links will
223 be copied as far as the platform allows; if false or omitted, the contents
224 and metadata of the linked files are copied to the new tree.
Georg Brandl86b2fb92008-07-16 03:43:04 +0000225
Tarek Ziadéfb437512010-04-20 08:57:33 +0000226 When *symlinks* is false, if the file pointed by the symlink doesn't
Martin Panter7462b6492015-11-02 03:37:02 +0000227 exist, an exception will be added in the list of errors raised in
228 an :exc:`Error` exception at the end of the copy process.
Tarek Ziadéfb437512010-04-20 08:57:33 +0000229 You can set the optional *ignore_dangling_symlinks* flag to true if you
Tarek Ziadé8c26c7d2010-04-23 13:03:50 +0000230 want to silence this exception. Notice that this option has no effect
231 on platforms that don't support :func:`os.symlink`.
Tarek Ziadéfb437512010-04-20 08:57:33 +0000232
Georg Brandl86b2fb92008-07-16 03:43:04 +0000233 If *ignore* is given, it must be a callable that will receive as its
234 arguments the directory being visited by :func:`copytree`, and a list of its
235 contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
236 called recursively, the *ignore* callable will be called once for each
237 directory that is copied. The callable must return a sequence of directory
238 and file names relative to the current directory (i.e. a subset of the items
239 in its second argument); these names will then be ignored in the copy
240 process. :func:`ignore_patterns` can be used to create such a callable that
241 ignores names based on glob-style patterns.
242
243 If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
244
Senthil Kumaran7f728c12012-02-13 23:30:47 +0800245 If *copy_function* is given, it must be a callable that will be used to copy
246 each file. It will be called with the source path and the destination path
247 as arguments. By default, :func:`shutil.copy2` is used, but any function
Senthil Kumaran1fd64822012-02-13 23:35:44 +0800248 that supports the same signature (like :func:`shutil.copy`) can be used.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700250 .. versionchanged:: 3.3
251 Copy metadata when *symlinks* is false.
252 Now returns *dst*.
253
Tarek Ziadé5340db32010-04-19 22:30:51 +0000254 .. versionchanged:: 3.2
255 Added the *copy_function* argument to be able to provide a custom copy
256 function.
Tarek Ziadéfb437512010-04-20 08:57:33 +0000257 Added the *ignore_dangling_symlinks* argument to silent dangling symlinks
258 errors when *symlinks* is false.
259
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200260 .. versionchanged:: 3.8
261 Platform-specific fast-copy syscalls may be used internally in order to
262 copy the file more efficiently. See
263 :ref:`shutil-platform-dependent-efficient-copy-operations` section.
Georg Brandl96acb732012-06-24 17:39:05 +0200264
Georg Brandl18244152009-09-02 20:34:52 +0000265.. function:: rmtree(path, ignore_errors=False, onerror=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267 .. index:: single: directory; deleting
268
Christian Heimes9bd667a2008-01-20 15:14:11 +0000269 Delete an entire directory tree; *path* must point to a directory (but not a
270 symbolic link to a directory). If *ignore_errors* is true, errors resulting
271 from failed removals will be ignored; if false or omitted, such errors are
272 handled by calling a handler specified by *onerror* or, if that is omitted,
273 they raise an exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Nick Coghlan5b0eca12012-06-24 16:43:06 +1000275 .. note::
Hynek Schlawack67be92b2012-06-23 17:58:42 +0200276
Nick Coghlan5b0eca12012-06-24 16:43:06 +1000277 On platforms that support the necessary fd-based functions a symlink
Georg Brandl96acb732012-06-24 17:39:05 +0200278 attack resistant version of :func:`rmtree` is used by default. On other
279 platforms, the :func:`rmtree` implementation is susceptible to a symlink
280 attack: given proper timing and circumstances, attackers can manipulate
281 symlinks on the filesystem to delete files they wouldn't be able to access
282 otherwise. Applications can use the :data:`rmtree.avoids_symlink_attacks`
283 function attribute to determine which case applies.
Hynek Schlawack67be92b2012-06-23 17:58:42 +0200284
Christian Heimes9bd667a2008-01-20 15:14:11 +0000285 If *onerror* is provided, it must be a callable that accepts three
Hynek Schlawack67be92b2012-06-23 17:58:42 +0200286 parameters: *function*, *path*, and *excinfo*.
287
288 The first parameter, *function*, is the function which raised the exception;
289 it depends on the platform and implementation. The second parameter,
290 *path*, will be the path name passed to *function*. The third parameter,
291 *excinfo*, will be the exception information returned by
292 :func:`sys.exc_info`. Exceptions raised by *onerror* will not be caught.
293
294 .. versionchanged:: 3.3
Nick Coghlan5b0eca12012-06-24 16:43:06 +1000295 Added a symlink attack resistant version that is used automatically
296 if platform supports fd-based functions.
Christian Heimes9bd667a2008-01-20 15:14:11 +0000297
Éric Araujo544e13d2012-06-24 13:53:48 -0400298 .. attribute:: rmtree.avoids_symlink_attacks
Hynek Schlawack2100b422012-06-23 20:28:32 +0200299
Nick Coghlan5b0eca12012-06-24 16:43:06 +1000300 Indicates whether the current platform and implementation provides a
Georg Brandl96acb732012-06-24 17:39:05 +0200301 symlink attack resistant version of :func:`rmtree`. Currently this is
Nick Coghlan5b0eca12012-06-24 16:43:06 +1000302 only true for platforms supporting fd-based directory access functions.
Hynek Schlawack2100b422012-06-23 20:28:32 +0200303
Nick Coghlan5b0eca12012-06-24 16:43:06 +1000304 .. versionadded:: 3.3
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Georg Brandl96acb732012-06-24 17:39:05 +0200306
R David Murray6ffface2014-06-11 14:40:13 -0400307.. function:: move(src, dst, copy_function=copy2)
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Brian Curtin0d0a1de2012-06-18 18:41:07 -0500309 Recursively move a file or directory (*src*) to another location (*dst*)
310 and return the destination.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
Benjamin Peterson218144a2015-03-22 10:11:54 -0400312 If the destination is an existing directory, then *src* is moved inside that
313 directory. If the destination already exists but is not a directory, it may
314 be overwritten depending on :func:`os.rename` semantics.
Éric Araujo14382dc2011-07-28 22:49:11 +0200315
316 If the destination is on the current filesystem, then :func:`os.rename` is
R David Murray6ffface2014-06-11 14:40:13 -0400317 used. Otherwise, *src* is copied to *dst* using *copy_function* and then
318 removed. In case of symlinks, a new symlink pointing to the target of *src*
319 will be created in or as *dst* and *src* will be removed.
320
321 If *copy_function* is given, it must be a callable that takes two arguments
322 *src* and *dst*, and will be used to copy *src* to *dest* if
323 :func:`os.rename` cannot be used. If the source is a directory,
324 :func:`copytree` is called, passing it the :func:`copy_function`. The
Mariatta70ee0cd2017-03-10 18:17:21 -0800325 default *copy_function* is :func:`copy2`. Using :func:`~shutil.copy` as the
R David Murray6ffface2014-06-11 14:40:13 -0400326 *copy_function* allows the move to succeed when it is not possible to also
327 copy the metadata, at the expense of not copying any of the metadata.
Antoine Pitrou0a08d7a2012-01-06 20:16:19 +0100328
329 .. versionchanged:: 3.3
330 Added explicit symlink handling for foreign filesystems, thus adapting
331 it to the behavior of GNU's :program:`mv`.
Larry Hastings7aa2c8b2012-07-15 16:58:29 -0700332 Now returns *dst*.
Brian Curtin066dacf2012-06-19 10:03:05 -0500333
R David Murray6ffface2014-06-11 14:40:13 -0400334 .. versionchanged:: 3.5
335 Added the *copy_function* keyword argument.
336
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200337 .. versionchanged:: 3.8
338 Platform-specific fast-copy syscalls may be used internally in order to
339 copy the file more efficiently. See
340 :ref:`shutil-platform-dependent-efficient-copy-operations` section.
341
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +0200342.. function:: disk_usage(path)
343
Éric Araujoe4d5b8e2011-08-08 16:51:11 +0200344 Return disk usage statistics about the given path as a :term:`named tuple`
345 with the attributes *total*, *used* and *free*, which are the amount of
Joe Pamerc8c02492018-09-25 10:57:36 -0400346 total, used and free space, in bytes. *path* may be a file or a
347 directory.
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +0200348
349 .. versionadded:: 3.3
350
Joe Pamerc8c02492018-09-25 10:57:36 -0400351 .. versionchanged:: 3.8
352 On Windows, *path* can now be a file or directory.
353
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +0200354 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000355
Sandro Tosid902a142011-08-22 23:28:27 +0200356.. function:: chown(path, user=None, group=None)
357
358 Change owner *user* and/or *group* of the given *path*.
359
360 *user* can be a system user name or a uid; the same applies to *group*. At
361 least one argument is required.
362
363 See also :func:`os.chown`, the underlying function.
364
365 Availability: Unix.
366
367 .. versionadded:: 3.3
368
Georg Brandl4a7e25f2012-06-24 17:37:07 +0200369
Brian Curtinc57a3452012-06-22 16:00:30 -0500370.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)
371
Georg Brandl4a7e25f2012-06-24 17:37:07 +0200372 Return the path to an executable which would be run if the given *cmd* was
373 called. If no *cmd* would be called, return ``None``.
Brian Curtinc57a3452012-06-22 16:00:30 -0500374
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300375 *mode* is a permission mask passed to :func:`os.access`, by default
Brian Curtinc57a3452012-06-22 16:00:30 -0500376 determining if the file exists and executable.
377
Georg Brandl4a7e25f2012-06-24 17:37:07 +0200378 When no *path* is specified, the results of :func:`os.environ` are used,
379 returning either the "PATH" value or a fallback of :attr:`os.defpath`.
Brian Curtinc57a3452012-06-22 16:00:30 -0500380
Georg Brandl4a7e25f2012-06-24 17:37:07 +0200381 On Windows, the current directory is always prepended to the *path* whether
382 or not you use the default or provide your own, which is the behavior the
Donald Stufft8b852f12014-05-20 12:58:38 -0400383 command shell uses when finding executables. Additionally, when finding the
Georg Brandl4a7e25f2012-06-24 17:37:07 +0200384 *cmd* in the *path*, the ``PATHEXT`` environment variable is checked. For
385 example, if you call ``shutil.which("python")``, :func:`which` will search
386 ``PATHEXT`` to know that it should look for ``python.exe`` within the *path*
387 directories. For example, on Windows::
Brian Curtinc57a3452012-06-22 16:00:30 -0500388
Georg Brandl4a7e25f2012-06-24 17:37:07 +0200389 >>> shutil.which("python")
Serhiy Storchaka80c88f42013-01-22 10:31:36 +0200390 'C:\\Python33\\python.EXE'
Brian Curtinc57a3452012-06-22 16:00:30 -0500391
392 .. versionadded:: 3.3
Sandro Tosid902a142011-08-22 23:28:27 +0200393
Georg Brandl4a7e25f2012-06-24 17:37:07 +0200394
Georg Brandl116aa622007-08-15 14:28:22 +0000395.. exception:: Error
396
Éric Araujo14382dc2011-07-28 22:49:11 +0200397 This exception collects exceptions that are raised during a multi-file
398 operation. For :func:`copytree`, the exception argument is a list of 3-tuples
399 (*srcname*, *dstname*, *exception*).
Georg Brandl116aa622007-08-15 14:28:22 +0000400
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200401.. _shutil-platform-dependent-efficient-copy-operations:
402
403Platform-dependent efficient copy operations
404~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
405
406Starting from Python 3.8 all functions involving a file copy (:func:`copyfile`,
407:func:`copy`, :func:`copy2`, :func:`copytree`, and :func:`move`) may use
408platform-specific "fast-copy" syscalls in order to copy the file more
409efficiently (see :issue:`33671`).
410"fast-copy" means that the copying operation occurs within the kernel, avoiding
411the use of userspace buffers in Python as in "``outfd.write(infd.read())``".
412
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -0700413On macOS `fcopyfile`_ is used to copy the file content (not metadata).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200414
415On Linux, Solaris and other POSIX platforms where :func:`os.sendfile` supports
416copies between 2 regular file descriptors :func:`os.sendfile` is used.
417
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -0700418On Windows :func:`shutil.copyfile` uses a bigger default buffer size (1 MiB
419instead of 16 KiB) and a :func:`memoryview`-based variant of
420:func:`shutil.copyfileobj` is used.
421
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200422If the fast-copy operation fails and no data was written in the destination
423file then shutil will silently fallback on using less efficient
424:func:`copyfileobj` function internally.
425
426.. versionchanged:: 3.8
Georg Brandl116aa622007-08-15 14:28:22 +0000427
Éric Araujof2fbb9c2012-01-16 16:55:55 +0100428.. _shutil-copytree-example:
Georg Brandl116aa622007-08-15 14:28:22 +0000429
Tarek Ziadé396fad72010-02-23 05:30:31 +0000430copytree example
Georg Brandl03b9ad02012-06-24 18:09:40 +0200431~~~~~~~~~~~~~~~~
Georg Brandl116aa622007-08-15 14:28:22 +0000432
433This example is the implementation of the :func:`copytree` function, described
434above, with the docstring omitted. It demonstrates many of the other functions
435provided by this module. ::
436
437 def copytree(src, dst, symlinks=False):
438 names = os.listdir(src)
439 os.makedirs(dst)
440 errors = []
441 for name in names:
442 srcname = os.path.join(src, name)
443 dstname = os.path.join(dst, name)
444 try:
445 if symlinks and os.path.islink(srcname):
446 linkto = os.readlink(srcname)
447 os.symlink(linkto, dstname)
448 elif os.path.isdir(srcname):
449 copytree(srcname, dstname, symlinks)
450 else:
451 copy2(srcname, dstname)
452 # XXX What about devices, sockets etc.?
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200453 except OSError as why:
Georg Brandl116aa622007-08-15 14:28:22 +0000454 errors.append((srcname, dstname, str(why)))
455 # catch the Error from the recursive copytree so that we can
456 # continue with other files
457 except Error as err:
458 errors.extend(err.args[0])
459 try:
460 copystat(src, dst)
Georg Brandl116aa622007-08-15 14:28:22 +0000461 except OSError as why:
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200462 # can't copy file access times on Windows
463 if why.winerror is None:
464 errors.extend((src, dst, str(why)))
Georg Brandl116aa622007-08-15 14:28:22 +0000465 if errors:
Collin Winterc79461b2007-09-01 23:34:30 +0000466 raise Error(errors)
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Tarek Ziadé396fad72010-02-23 05:30:31 +0000468Another example that uses the :func:`ignore_patterns` helper::
469
470 from shutil import copytree, ignore_patterns
471
472 copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
473
474This will copy everything except ``.pyc`` files and files or directories whose
475name starts with ``tmp``.
476
477Another example that uses the *ignore* argument to add a logging call::
478
479 from shutil import copytree
480 import logging
481
482 def _logpath(path, names):
Vinay Sajipdd917f82016-08-31 08:22:29 +0100483 logging.info('Working in %s', path)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000484 return [] # nothing will be ignored
485
486 copytree(source, destination, ignore=_logpath)
487
488
Tim Golden78337792014-05-07 18:05:45 +0100489.. _shutil-rmtree-example:
490
491rmtree example
492~~~~~~~~~~~~~~
493
494This example shows how to remove a directory tree on Windows where some
495of the files have their read-only bit set. It uses the onerror callback
496to clear the readonly bit and reattempt the remove. Any subsequent failure
497will propagate. ::
498
499 import os, stat
500 import shutil
Tim Goldenba748852014-05-07 18:08:08 +0100501
Tim Golden78337792014-05-07 18:05:45 +0100502 def remove_readonly(func, path, _):
503 "Clear the readonly bit and reattempt the removal"
504 os.chmod(path, stat.S_IWRITE)
Tim Goldenba748852014-05-07 18:08:08 +0100505 func(path)
506
Tim Golden78337792014-05-07 18:05:45 +0100507 shutil.rmtree(directory, onerror=remove_readonly)
508
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000509.. _archiving-operations:
510
511Archiving operations
512--------------------
Tarek Ziadé396fad72010-02-23 05:30:31 +0000513
Georg Brandl03b9ad02012-06-24 18:09:40 +0200514.. versionadded:: 3.2
515
Serhiy Storchaka20cdffd2016-12-16 18:58:33 +0200516.. versionchanged:: 3.5
517 Added support for the *xztar* format.
518
519
Éric Araujof2fbb9c2012-01-16 16:55:55 +0100520High-level utilities to create and read compressed and archived files are also
521provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
522
Tarek Ziadé396fad72010-02-23 05:30:31 +0000523.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
524
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000525 Create an archive file (such as zip or tar) and return its name.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000526
527 *base_name* is the name of the file to create, including the path, minus
528 any format-specific extension. *format* is the archive format: one of
Serhiy Storchaka20cdffd2016-12-16 18:58:33 +0200529 "zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the
530 :mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is
531 available), or "xztar" (if the :mod:`lzma` module is available).
Tarek Ziadé396fad72010-02-23 05:30:31 +0000532
533 *root_dir* is a directory that will be the root directory of the
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000534 archive; for example, we typically chdir into *root_dir* before creating the
Tarek Ziadé396fad72010-02-23 05:30:31 +0000535 archive.
536
537 *base_dir* is the directory where we start archiving from;
Ezio Melotticb999a32010-04-20 11:26:51 +0000538 i.e. *base_dir* will be the common prefix of all files and
Tarek Ziadé396fad72010-02-23 05:30:31 +0000539 directories in the archive.
540
541 *root_dir* and *base_dir* both default to the current directory.
542
Georg Brandl9b1b0e52014-10-31 10:02:40 +0100543 If *dry_run* is true, no archive is created, but the operations that would be
544 executed are logged to *logger*.
545
Tarek Ziadé396fad72010-02-23 05:30:31 +0000546 *owner* and *group* are used when creating a tar archive. By default,
547 uses the current owner and group.
548
Éric Araujo06c42a32011-11-07 17:31:07 +0100549 *logger* must be an object compatible with :pep:`282`, usually an instance of
550 :class:`logging.Logger`.
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000551
Georg Brandl36ac5102014-10-31 10:54:06 +0100552 The *verbose* argument is unused and deprecated.
Georg Brandl9b1b0e52014-10-31 10:02:40 +0100553
Tarek Ziadé396fad72010-02-23 05:30:31 +0000554
555.. function:: get_archive_formats()
556
Éric Araujo14382dc2011-07-28 22:49:11 +0200557 Return a list of supported formats for archiving.
Martin Panterd21e0b52015-10-10 10:36:22 +0000558 Each element of the returned sequence is a tuple ``(name, description)``.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000559
560 By default :mod:`shutil` provides these formats:
561
Serhiy Storchaka20cdffd2016-12-16 18:58:33 +0200562 - *zip*: ZIP file (if the :mod:`zlib` module is available).
563 - *tar*: uncompressed tar file.
564 - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
565 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
566 - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
Tarek Ziadé396fad72010-02-23 05:30:31 +0000567
568 You can register new formats or provide your own archiver for any existing
569 formats, by using :func:`register_archive_format`.
570
Tarek Ziadé396fad72010-02-23 05:30:31 +0000571
572.. function:: register_archive_format(name, function, [extra_args, [description]])
573
Georg Brandl9b1b0e52014-10-31 10:02:40 +0100574 Register an archiver for the format *name*.
575
576 *function* is the callable that will be used to unpack archives. The callable
577 will receive the *base_name* of the file to create, followed by the
578 *base_dir* (which defaults to :data:`os.curdir`) to start archiving from.
579 Further arguments are passed as keyword arguments: *owner*, *group*,
580 *dry_run* and *logger* (as passed in :func:`make_archive`).
Tarek Ziadé396fad72010-02-23 05:30:31 +0000581
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000582 If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be
Tarek Ziadé396fad72010-02-23 05:30:31 +0000583 used as extra keywords arguments when the archiver callable is used.
584
585 *description* is used by :func:`get_archive_formats` which returns the
Georg Brandl9b1b0e52014-10-31 10:02:40 +0100586 list of archivers. Defaults to an empty string.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000587
Tarek Ziadé396fad72010-02-23 05:30:31 +0000588
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000589.. function:: unregister_archive_format(name)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000590
591 Remove the archive format *name* from the list of supported formats.
592
Tarek Ziadé396fad72010-02-23 05:30:31 +0000593
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000594.. function:: unpack_archive(filename[, extract_dir[, format]])
595
596 Unpack an archive. *filename* is the full path of the archive.
597
598 *extract_dir* is the name of the target directory where the archive is
599 unpacked. If not provided, the current working directory is used.
600
Serhiy Storchaka20cdffd2016-12-16 18:58:33 +0200601 *format* is the archive format: one of "zip", "tar", "gztar", "bztar", or
602 "xztar". Or any other format registered with
603 :func:`register_unpack_format`. If not provided, :func:`unpack_archive`
604 will use the archive file name extension and see if an unpacker was
605 registered for that extension. In case none is found,
606 a :exc:`ValueError` is raised.
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000607
Jelle Zijlstraa12df7b2017-05-05 14:27:12 -0700608 .. versionchanged:: 3.7
609 Accepts a :term:`path-like object` for *filename* and *extract_dir*.
610
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000611
Raymond Hettinger0929b1f2011-01-23 11:29:08 +0000612.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000613
614 Registers an unpack format. *name* is the name of the format and
615 *extensions* is a list of extensions corresponding to the format, like
616 ``.zip`` for Zip files.
617
618 *function* is the callable that will be used to unpack archives. The
619 callable will receive the path of the archive, followed by the directory
620 the archive must be extracted to.
621
622 When provided, *extra_args* is a sequence of ``(name, value)`` tuples that
623 will be passed as keywords arguments to the callable.
624
625 *description* can be provided to describe the format, and will be returned
626 by the :func:`get_unpack_formats` function.
627
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000628
629.. function:: unregister_unpack_format(name)
630
631 Unregister an unpack format. *name* is the name of the format.
632
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000633
634.. function:: get_unpack_formats()
635
636 Return a list of all registered formats for unpacking.
637 Each element of the returned sequence is a tuple
638 ``(name, extensions, description)``.
639
640 By default :mod:`shutil` provides these formats:
641
Martin Panter2f9171d2016-12-18 01:23:09 +0000642 - *zip*: ZIP file (unpacking compressed files works only if the corresponding
Serhiy Storchaka20cdffd2016-12-16 18:58:33 +0200643 module is available).
644 - *tar*: uncompressed tar file.
645 - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
646 - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
647 - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000648
649 You can register new formats or provide your own unpacker for any existing
650 formats, by using :func:`register_unpack_format`.
651
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000652
Éric Araujof2fbb9c2012-01-16 16:55:55 +0100653.. _shutil-archiving-example:
Tarek Ziadé6ac91722010-04-28 17:51:36 +0000654
Tarek Ziadé396fad72010-02-23 05:30:31 +0000655Archiving example
Georg Brandl03b9ad02012-06-24 18:09:40 +0200656~~~~~~~~~~~~~~~~~
Tarek Ziadé396fad72010-02-23 05:30:31 +0000657
658In this example, we create a gzip'ed tar-file archive containing all files
659found in the :file:`.ssh` directory of the user::
660
661 >>> from shutil import make_archive
662 >>> import os
663 >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
664 >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
665 >>> make_archive(archive_name, 'gztar', root_dir)
666 '/Users/tarek/myarchive.tar.gz'
667
Martin Panter1050d2d2016-07-26 11:18:21 +0200668The resulting archive contains:
669
670.. code-block:: shell-session
Tarek Ziadé396fad72010-02-23 05:30:31 +0000671
672 $ tar -tzvf /Users/tarek/myarchive.tar.gz
673 drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
674 -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
675 -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
676 -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
677 -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
678 -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
679 -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
680 -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100681
682
683Querying the size of the output terminal
684----------------------------------------
685
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100686.. function:: get_terminal_size(fallback=(columns, lines))
687
688 Get the size of the terminal window.
689
690 For each of the two dimensions, the environment variable, ``COLUMNS``
691 and ``LINES`` respectively, is checked. If the variable is defined and
692 the value is a positive integer, it is used.
693
694 When ``COLUMNS`` or ``LINES`` is not defined, which is the common case,
695 the terminal connected to :data:`sys.__stdout__` is queried
696 by invoking :func:`os.get_terminal_size`.
697
698 If the terminal size cannot be successfully queried, either because
699 the system doesn't support querying, or because we are not
700 connected to a terminal, the value given in ``fallback`` parameter
701 is used. ``fallback`` defaults to ``(80, 24)`` which is the default
702 size used by many terminal emulators.
703
704 The value returned is a named tuple of type :class:`os.terminal_size`.
705
706 See also: The Single UNIX Specification, Version 2,
707 `Other Environment Variables`_.
708
Berker Peksag8e2bdc82016-12-27 15:09:11 +0300709 .. versionadded:: 3.3
710
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200711.. _`fcopyfile`:
712 http://www.manpagez.com/man/3/copyfile/
713
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100714.. _`Other Environment Variables`:
715 http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003