Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`shutil` --- High-level file operations |
| 2 | ============================================ |
| 3 | |
| 4 | .. module:: shutil |
| 5 | :synopsis: High-level file operations, including copying. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 8 | .. partly based on the docstrings |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | **Source code:** :source:`Lib/shutil.py` |
| 11 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | .. index:: |
| 13 | single: file; copying |
| 14 | single: copying files |
| 15 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 16 | -------------- |
| 17 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | The :mod:`shutil` module offers a number of high-level operations on files and |
| 19 | collections of files. In particular, functions are provided which support file |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 20 | copying and removal. For operations on individual files, see also the |
| 21 | :mod:`os` module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | da27fd2 | 2007-08-17 00:24:54 +0000 | [diff] [blame] | 23 | .. warning:: |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 24 | |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 25 | Even the higher-level file copying functions (:func:`shutil.copy`, |
| 26 | :func:`shutil.copy2`) cannot copy all file metadata. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 27 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 28 | On POSIX platforms, this means that file owner and group are lost as well |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 29 | as ACLs. On Mac OS, the resource fork and other metadata are not used. |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 30 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 34 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 35 | .. _file-operations: |
| 36 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 37 | Directory and files operations |
| 38 | ------------------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | .. 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 Hastings | b403806 | 2012-07-15 10:57:38 -0700 | [diff] [blame] | 51 | .. function:: copyfile(src, dst, *, follow_symlinks=True) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 52 | |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 53 | Copy the contents (no metadata) of the file named *src* to a file named |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 54 | *dst* and return *dst* in the most efficient way possible. |
| 55 | *src* and *dst* are path names given as strings. |
| 56 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 57 | *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 Schlawack | 4865376 | 2012-10-07 12:49:58 +0200 | [diff] [blame] | 59 | specify the same file, :exc:`SameFileError` is raised. |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 60 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 61 | 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 Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 65 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 66 | 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 Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 69 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 70 | .. versionchanged:: 3.3 |
| 71 | :exc:`IOError` used to be raised instead of :exc:`OSError`. |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 72 | Added *follow_symlinks* argument. |
| 73 | Now returns *dst*. |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 74 | |
Hynek Schlawack | 4865376 | 2012-10-07 12:49:58 +0200 | [diff] [blame] | 75 | .. versionchanged:: 3.4 |
Hynek Schlawack | 27ddb57 | 2012-10-28 13:59:27 +0100 | [diff] [blame] | 76 | Raise :exc:`SameFileError` instead of :exc:`Error`. Since the former is |
| 77 | a subclass of the latter, this change is backward compatible. |
Hynek Schlawack | 4865376 | 2012-10-07 12:49:58 +0200 | [diff] [blame] | 78 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 79 | .. 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 Schlawack | 4865376 | 2012-10-07 12:49:58 +0200 | [diff] [blame] | 83 | |
| 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 Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 92 | .. function:: copymode(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | Copy the permission bits from *src* to *dst*. The file contents, owner, and |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 95 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 103 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 104 | Added *follow_symlinks* argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 106 | .. function:: copystat(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 108 | 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 Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 116 | symbolic links refer to—reading the information from the |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 117 | *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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 147 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 148 | Added *follow_symlinks* argument and support for Linux extended attributes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 150 | .. function:: copy(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 152 | 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 | |
Mariatta | 70ee0cd | 2017-03-10 18:17:21 -0800 | [diff] [blame] | 162 | :func:`~shutil.copy` copies the file data and the file's permission |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 163 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 168 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 169 | Added *follow_symlinks* argument. |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 170 | Now returns path to the newly created file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 172 | .. 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 Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 177 | .. function:: copy2(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 178 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 179 | Identical to :func:`~shutil.copy` except that :func:`copy2` |
Zsolt Cserna | 4f399be | 2018-10-23 12:09:50 +0200 | [diff] [blame] | 180 | also attempts to preserve file metadata. |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 194 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 195 | Added *follow_symlinks* argument, try to copy extended |
| 196 | file system attributes too (currently Linux only). |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 197 | Now returns path to the newly created file. |
Brian Curtin | 066dacf | 2012-06-19 10:03:05 -0500 | [diff] [blame] | 198 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 199 | .. 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 Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 204 | .. 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 Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 211 | .. function:: copytree(src, dst, symlinks=False, ignore=None, \ |
jab | 9e00d9e | 2018-12-28 13:03:40 -0500 | [diff] [blame] | 212 | copy_function=copy2, ignore_dangling_symlinks=False, \ |
| 213 | dirs_exist_ok=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 | |
jab | 9e00d9e | 2018-12-28 13:03:40 -0500 | [diff] [blame] | 215 | Recursively copy an entire directory tree rooted at *src* to a directory |
| 216 | named *dst* and return the destination directory. *dirs_exist_ok* dictates |
| 217 | whether to raise an exception in case *dst* or any missing parent directory |
| 218 | already exists. |
| 219 | |
| 220 | Permissions and times of directories are copied with :func:`copystat`, |
| 221 | individual files are copied using :func:`shutil.copy2`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 222 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 223 | If *symlinks* is true, symbolic links in the source tree are represented as |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 224 | symbolic links in the new tree and the metadata of the original links will |
| 225 | be copied as far as the platform allows; if false or omitted, the contents |
| 226 | and metadata of the linked files are copied to the new tree. |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 227 | |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 228 | When *symlinks* is false, if the file pointed by the symlink doesn't |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 229 | exist, an exception will be added in the list of errors raised in |
| 230 | an :exc:`Error` exception at the end of the copy process. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 231 | You can set the optional *ignore_dangling_symlinks* flag to true if you |
Tarek Ziadé | 8c26c7d | 2010-04-23 13:03:50 +0000 | [diff] [blame] | 232 | want to silence this exception. Notice that this option has no effect |
| 233 | on platforms that don't support :func:`os.symlink`. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 234 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 235 | If *ignore* is given, it must be a callable that will receive as its |
| 236 | arguments the directory being visited by :func:`copytree`, and a list of its |
| 237 | contents, as returned by :func:`os.listdir`. Since :func:`copytree` is |
| 238 | called recursively, the *ignore* callable will be called once for each |
| 239 | directory that is copied. The callable must return a sequence of directory |
| 240 | and file names relative to the current directory (i.e. a subset of the items |
| 241 | in its second argument); these names will then be ignored in the copy |
| 242 | process. :func:`ignore_patterns` can be used to create such a callable that |
| 243 | ignores names based on glob-style patterns. |
| 244 | |
| 245 | If exception(s) occur, an :exc:`Error` is raised with a list of reasons. |
| 246 | |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 247 | If *copy_function* is given, it must be a callable that will be used to copy |
| 248 | each file. It will be called with the source path and the destination path |
| 249 | as arguments. By default, :func:`shutil.copy2` is used, but any function |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 250 | that supports the same signature (like :func:`shutil.copy`) can be used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 252 | .. versionchanged:: 3.3 |
| 253 | Copy metadata when *symlinks* is false. |
| 254 | Now returns *dst*. |
| 255 | |
Tarek Ziadé | 5340db3 | 2010-04-19 22:30:51 +0000 | [diff] [blame] | 256 | .. versionchanged:: 3.2 |
| 257 | Added the *copy_function* argument to be able to provide a custom copy |
| 258 | function. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 259 | Added the *ignore_dangling_symlinks* argument to silent dangling symlinks |
| 260 | errors when *symlinks* is false. |
| 261 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 262 | .. versionchanged:: 3.8 |
| 263 | Platform-specific fast-copy syscalls may be used internally in order to |
| 264 | copy the file more efficiently. See |
| 265 | :ref:`shutil-platform-dependent-efficient-copy-operations` section. |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 266 | |
jab | 9e00d9e | 2018-12-28 13:03:40 -0500 | [diff] [blame] | 267 | .. versionadded:: 3.8 |
| 268 | The *dirs_exist_ok* parameter. |
| 269 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 270 | .. function:: rmtree(path, ignore_errors=False, onerror=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 271 | |
| 272 | .. index:: single: directory; deleting |
| 273 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 274 | Delete an entire directory tree; *path* must point to a directory (but not a |
| 275 | symbolic link to a directory). If *ignore_errors* is true, errors resulting |
| 276 | from failed removals will be ignored; if false or omitted, such errors are |
| 277 | handled by calling a handler specified by *onerror* or, if that is omitted, |
| 278 | they raise an exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 280 | .. note:: |
Hynek Schlawack | 67be92b | 2012-06-23 17:58:42 +0200 | [diff] [blame] | 281 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 282 | On platforms that support the necessary fd-based functions a symlink |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 283 | attack resistant version of :func:`rmtree` is used by default. On other |
| 284 | platforms, the :func:`rmtree` implementation is susceptible to a symlink |
| 285 | attack: given proper timing and circumstances, attackers can manipulate |
| 286 | symlinks on the filesystem to delete files they wouldn't be able to access |
| 287 | otherwise. Applications can use the :data:`rmtree.avoids_symlink_attacks` |
| 288 | function attribute to determine which case applies. |
Hynek Schlawack | 67be92b | 2012-06-23 17:58:42 +0200 | [diff] [blame] | 289 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 290 | If *onerror* is provided, it must be a callable that accepts three |
Hynek Schlawack | 67be92b | 2012-06-23 17:58:42 +0200 | [diff] [blame] | 291 | parameters: *function*, *path*, and *excinfo*. |
| 292 | |
| 293 | The first parameter, *function*, is the function which raised the exception; |
| 294 | it depends on the platform and implementation. The second parameter, |
| 295 | *path*, will be the path name passed to *function*. The third parameter, |
| 296 | *excinfo*, will be the exception information returned by |
| 297 | :func:`sys.exc_info`. Exceptions raised by *onerror* will not be caught. |
| 298 | |
| 299 | .. versionchanged:: 3.3 |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 300 | Added a symlink attack resistant version that is used automatically |
| 301 | if platform supports fd-based functions. |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 302 | |
Éric Araujo | 544e13d | 2012-06-24 13:53:48 -0400 | [diff] [blame] | 303 | .. attribute:: rmtree.avoids_symlink_attacks |
Hynek Schlawack | 2100b42 | 2012-06-23 20:28:32 +0200 | [diff] [blame] | 304 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 305 | Indicates whether the current platform and implementation provides a |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 306 | symlink attack resistant version of :func:`rmtree`. Currently this is |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 307 | only true for platforms supporting fd-based directory access functions. |
Hynek Schlawack | 2100b42 | 2012-06-23 20:28:32 +0200 | [diff] [blame] | 308 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 309 | .. versionadded:: 3.3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 311 | |
R David Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 312 | .. function:: move(src, dst, copy_function=copy2) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
Brian Curtin | 0d0a1de | 2012-06-18 18:41:07 -0500 | [diff] [blame] | 314 | Recursively move a file or directory (*src*) to another location (*dst*) |
| 315 | and return the destination. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | |
Benjamin Peterson | 218144a | 2015-03-22 10:11:54 -0400 | [diff] [blame] | 317 | If the destination is an existing directory, then *src* is moved inside that |
| 318 | directory. If the destination already exists but is not a directory, it may |
| 319 | be overwritten depending on :func:`os.rename` semantics. |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 320 | |
| 321 | If the destination is on the current filesystem, then :func:`os.rename` is |
R David Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 322 | used. Otherwise, *src* is copied to *dst* using *copy_function* and then |
| 323 | removed. In case of symlinks, a new symlink pointing to the target of *src* |
| 324 | will be created in or as *dst* and *src* will be removed. |
| 325 | |
| 326 | If *copy_function* is given, it must be a callable that takes two arguments |
| 327 | *src* and *dst*, and will be used to copy *src* to *dest* if |
| 328 | :func:`os.rename` cannot be used. If the source is a directory, |
| 329 | :func:`copytree` is called, passing it the :func:`copy_function`. The |
Mariatta | 70ee0cd | 2017-03-10 18:17:21 -0800 | [diff] [blame] | 330 | default *copy_function* is :func:`copy2`. Using :func:`~shutil.copy` as the |
R David Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 331 | *copy_function* allows the move to succeed when it is not possible to also |
| 332 | copy the metadata, at the expense of not copying any of the metadata. |
Antoine Pitrou | 0a08d7a | 2012-01-06 20:16:19 +0100 | [diff] [blame] | 333 | |
| 334 | .. versionchanged:: 3.3 |
| 335 | Added explicit symlink handling for foreign filesystems, thus adapting |
| 336 | it to the behavior of GNU's :program:`mv`. |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 337 | Now returns *dst*. |
Brian Curtin | 066dacf | 2012-06-19 10:03:05 -0500 | [diff] [blame] | 338 | |
R David Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 339 | .. versionchanged:: 3.5 |
| 340 | Added the *copy_function* keyword argument. |
| 341 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 342 | .. versionchanged:: 3.8 |
| 343 | Platform-specific fast-copy syscalls may be used internally in order to |
| 344 | copy the file more efficiently. See |
| 345 | :ref:`shutil-platform-dependent-efficient-copy-operations` section. |
| 346 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 347 | .. function:: disk_usage(path) |
| 348 | |
Éric Araujo | e4d5b8e | 2011-08-08 16:51:11 +0200 | [diff] [blame] | 349 | Return disk usage statistics about the given path as a :term:`named tuple` |
| 350 | with the attributes *total*, *used* and *free*, which are the amount of |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 351 | total, used and free space, in bytes. *path* may be a file or a |
| 352 | directory. |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 353 | |
| 354 | .. versionadded:: 3.3 |
| 355 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 356 | .. versionchanged:: 3.8 |
| 357 | On Windows, *path* can now be a file or directory. |
| 358 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 359 | .. availability:: Unix, Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 360 | |
Sandro Tosi | d902a14 | 2011-08-22 23:28:27 +0200 | [diff] [blame] | 361 | .. function:: chown(path, user=None, group=None) |
| 362 | |
| 363 | Change owner *user* and/or *group* of the given *path*. |
| 364 | |
| 365 | *user* can be a system user name or a uid; the same applies to *group*. At |
| 366 | least one argument is required. |
| 367 | |
| 368 | See also :func:`os.chown`, the underlying function. |
| 369 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 370 | .. availability:: Unix. |
Sandro Tosi | d902a14 | 2011-08-22 23:28:27 +0200 | [diff] [blame] | 371 | |
| 372 | .. versionadded:: 3.3 |
| 373 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 374 | |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 375 | .. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None) |
| 376 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 377 | Return the path to an executable which would be run if the given *cmd* was |
| 378 | called. If no *cmd* would be called, return ``None``. |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 379 | |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 380 | *mode* is a permission mask passed to :func:`os.access`, by default |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 381 | determining if the file exists and executable. |
| 382 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 383 | When no *path* is specified, the results of :func:`os.environ` are used, |
| 384 | returning either the "PATH" value or a fallback of :attr:`os.defpath`. |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 385 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 386 | On Windows, the current directory is always prepended to the *path* whether |
| 387 | or not you use the default or provide your own, which is the behavior the |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 388 | command shell uses when finding executables. Additionally, when finding the |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 389 | *cmd* in the *path*, the ``PATHEXT`` environment variable is checked. For |
| 390 | example, if you call ``shutil.which("python")``, :func:`which` will search |
| 391 | ``PATHEXT`` to know that it should look for ``python.exe`` within the *path* |
| 392 | directories. For example, on Windows:: |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 393 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 394 | >>> shutil.which("python") |
Serhiy Storchaka | 80c88f4 | 2013-01-22 10:31:36 +0200 | [diff] [blame] | 395 | 'C:\\Python33\\python.EXE' |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 396 | |
| 397 | .. versionadded:: 3.3 |
Sandro Tosi | d902a14 | 2011-08-22 23:28:27 +0200 | [diff] [blame] | 398 | |
Cheryl Sabella | 5680f65 | 2019-02-13 06:25:10 -0500 | [diff] [blame] | 399 | .. versionchanged:: 3.8 |
| 400 | The :class:`bytes` type is now accepted. If *cmd* type is |
| 401 | :class:`bytes`, the result type is also :class:`bytes`. |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 402 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 403 | .. exception:: Error |
| 404 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 405 | This exception collects exceptions that are raised during a multi-file |
| 406 | operation. For :func:`copytree`, the exception argument is a list of 3-tuples |
| 407 | (*srcname*, *dstname*, *exception*). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 408 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 409 | .. _shutil-platform-dependent-efficient-copy-operations: |
| 410 | |
| 411 | Platform-dependent efficient copy operations |
| 412 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 413 | |
| 414 | Starting from Python 3.8 all functions involving a file copy (:func:`copyfile`, |
| 415 | :func:`copy`, :func:`copy2`, :func:`copytree`, and :func:`move`) may use |
| 416 | platform-specific "fast-copy" syscalls in order to copy the file more |
| 417 | efficiently (see :issue:`33671`). |
| 418 | "fast-copy" means that the copying operation occurs within the kernel, avoiding |
| 419 | the use of userspace buffers in Python as in "``outfd.write(infd.read())``". |
| 420 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 421 | On macOS `fcopyfile`_ is used to copy the file content (not metadata). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 422 | |
| 423 | On Linux, Solaris and other POSIX platforms where :func:`os.sendfile` supports |
| 424 | copies between 2 regular file descriptors :func:`os.sendfile` is used. |
| 425 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 426 | On Windows :func:`shutil.copyfile` uses a bigger default buffer size (1 MiB |
Inada Naoki | 4f19030 | 2019-03-02 13:31:01 +0900 | [diff] [blame] | 427 | instead of 64 KiB) and a :func:`memoryview`-based variant of |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 428 | :func:`shutil.copyfileobj` is used. |
| 429 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 430 | If the fast-copy operation fails and no data was written in the destination |
| 431 | file then shutil will silently fallback on using less efficient |
| 432 | :func:`copyfileobj` function internally. |
| 433 | |
| 434 | .. versionchanged:: 3.8 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 435 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 436 | .. _shutil-copytree-example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 437 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 438 | copytree example |
Georg Brandl | 03b9ad0 | 2012-06-24 18:09:40 +0200 | [diff] [blame] | 439 | ~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | |
| 441 | This example is the implementation of the :func:`copytree` function, described |
| 442 | above, with the docstring omitted. It demonstrates many of the other functions |
| 443 | provided by this module. :: |
| 444 | |
| 445 | def copytree(src, dst, symlinks=False): |
| 446 | names = os.listdir(src) |
| 447 | os.makedirs(dst) |
| 448 | errors = [] |
| 449 | for name in names: |
| 450 | srcname = os.path.join(src, name) |
| 451 | dstname = os.path.join(dst, name) |
| 452 | try: |
| 453 | if symlinks and os.path.islink(srcname): |
| 454 | linkto = os.readlink(srcname) |
| 455 | os.symlink(linkto, dstname) |
| 456 | elif os.path.isdir(srcname): |
| 457 | copytree(srcname, dstname, symlinks) |
| 458 | else: |
| 459 | copy2(srcname, dstname) |
| 460 | # XXX What about devices, sockets etc.? |
Andrew Svetlov | 618c2e1 | 2012-12-15 22:59:24 +0200 | [diff] [blame] | 461 | except OSError as why: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | errors.append((srcname, dstname, str(why))) |
| 463 | # catch the Error from the recursive copytree so that we can |
| 464 | # continue with other files |
| 465 | except Error as err: |
| 466 | errors.extend(err.args[0]) |
| 467 | try: |
| 468 | copystat(src, dst) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 469 | except OSError as why: |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 470 | # can't copy file access times on Windows |
| 471 | if why.winerror is None: |
| 472 | errors.extend((src, dst, str(why))) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 473 | if errors: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 474 | raise Error(errors) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 475 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 476 | Another example that uses the :func:`ignore_patterns` helper:: |
| 477 | |
| 478 | from shutil import copytree, ignore_patterns |
| 479 | |
| 480 | copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*')) |
| 481 | |
| 482 | This will copy everything except ``.pyc`` files and files or directories whose |
| 483 | name starts with ``tmp``. |
| 484 | |
| 485 | Another example that uses the *ignore* argument to add a logging call:: |
| 486 | |
| 487 | from shutil import copytree |
| 488 | import logging |
| 489 | |
| 490 | def _logpath(path, names): |
Vinay Sajip | dd917f8 | 2016-08-31 08:22:29 +0100 | [diff] [blame] | 491 | logging.info('Working in %s', path) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 492 | return [] # nothing will be ignored |
| 493 | |
| 494 | copytree(source, destination, ignore=_logpath) |
| 495 | |
| 496 | |
Tim Golden | 7833779 | 2014-05-07 18:05:45 +0100 | [diff] [blame] | 497 | .. _shutil-rmtree-example: |
| 498 | |
| 499 | rmtree example |
| 500 | ~~~~~~~~~~~~~~ |
| 501 | |
| 502 | This example shows how to remove a directory tree on Windows where some |
| 503 | of the files have their read-only bit set. It uses the onerror callback |
| 504 | to clear the readonly bit and reattempt the remove. Any subsequent failure |
| 505 | will propagate. :: |
| 506 | |
| 507 | import os, stat |
| 508 | import shutil |
Tim Golden | ba74885 | 2014-05-07 18:08:08 +0100 | [diff] [blame] | 509 | |
Tim Golden | 7833779 | 2014-05-07 18:05:45 +0100 | [diff] [blame] | 510 | def remove_readonly(func, path, _): |
| 511 | "Clear the readonly bit and reattempt the removal" |
| 512 | os.chmod(path, stat.S_IWRITE) |
Tim Golden | ba74885 | 2014-05-07 18:08:08 +0100 | [diff] [blame] | 513 | func(path) |
| 514 | |
Tim Golden | 7833779 | 2014-05-07 18:05:45 +0100 | [diff] [blame] | 515 | shutil.rmtree(directory, onerror=remove_readonly) |
| 516 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 517 | .. _archiving-operations: |
| 518 | |
| 519 | Archiving operations |
| 520 | -------------------- |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 521 | |
Georg Brandl | 03b9ad0 | 2012-06-24 18:09:40 +0200 | [diff] [blame] | 522 | .. versionadded:: 3.2 |
| 523 | |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 524 | .. versionchanged:: 3.5 |
| 525 | Added support for the *xztar* format. |
| 526 | |
| 527 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 528 | High-level utilities to create and read compressed and archived files are also |
| 529 | provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. |
| 530 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 531 | .. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]]) |
| 532 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 533 | Create an archive file (such as zip or tar) and return its name. |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 534 | |
| 535 | *base_name* is the name of the file to create, including the path, minus |
| 536 | any format-specific extension. *format* is the archive format: one of |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 537 | "zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the |
| 538 | :mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is |
| 539 | available), or "xztar" (if the :mod:`lzma` module is available). |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 540 | |
| 541 | *root_dir* is a directory that will be the root directory of the |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 542 | archive; for example, we typically chdir into *root_dir* before creating the |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 543 | archive. |
| 544 | |
| 545 | *base_dir* is the directory where we start archiving from; |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 546 | i.e. *base_dir* will be the common prefix of all files and |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 547 | directories in the archive. |
| 548 | |
| 549 | *root_dir* and *base_dir* both default to the current directory. |
| 550 | |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 551 | If *dry_run* is true, no archive is created, but the operations that would be |
| 552 | executed are logged to *logger*. |
| 553 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 554 | *owner* and *group* are used when creating a tar archive. By default, |
| 555 | uses the current owner and group. |
| 556 | |
Éric Araujo | 06c42a3 | 2011-11-07 17:31:07 +0100 | [diff] [blame] | 557 | *logger* must be an object compatible with :pep:`282`, usually an instance of |
| 558 | :class:`logging.Logger`. |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 559 | |
Georg Brandl | 36ac510 | 2014-10-31 10:54:06 +0100 | [diff] [blame] | 560 | The *verbose* argument is unused and deprecated. |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 561 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 562 | |
| 563 | .. function:: get_archive_formats() |
| 564 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 565 | Return a list of supported formats for archiving. |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 566 | Each element of the returned sequence is a tuple ``(name, description)``. |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 567 | |
| 568 | By default :mod:`shutil` provides these formats: |
| 569 | |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 570 | - *zip*: ZIP file (if the :mod:`zlib` module is available). |
| 571 | - *tar*: uncompressed tar file. |
| 572 | - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available). |
| 573 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available). |
| 574 | - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available). |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 575 | |
| 576 | You can register new formats or provide your own archiver for any existing |
| 577 | formats, by using :func:`register_archive_format`. |
| 578 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 579 | |
| 580 | .. function:: register_archive_format(name, function, [extra_args, [description]]) |
| 581 | |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 582 | Register an archiver for the format *name*. |
| 583 | |
| 584 | *function* is the callable that will be used to unpack archives. The callable |
| 585 | will receive the *base_name* of the file to create, followed by the |
| 586 | *base_dir* (which defaults to :data:`os.curdir`) to start archiving from. |
| 587 | Further arguments are passed as keyword arguments: *owner*, *group*, |
| 588 | *dry_run* and *logger* (as passed in :func:`make_archive`). |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 589 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 590 | If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 591 | used as extra keywords arguments when the archiver callable is used. |
| 592 | |
| 593 | *description* is used by :func:`get_archive_formats` which returns the |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 594 | list of archivers. Defaults to an empty string. |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 595 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 596 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 597 | .. function:: unregister_archive_format(name) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 598 | |
| 599 | Remove the archive format *name* from the list of supported formats. |
| 600 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 601 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 602 | .. function:: unpack_archive(filename[, extract_dir[, format]]) |
| 603 | |
| 604 | Unpack an archive. *filename* is the full path of the archive. |
| 605 | |
| 606 | *extract_dir* is the name of the target directory where the archive is |
| 607 | unpacked. If not provided, the current working directory is used. |
| 608 | |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 609 | *format* is the archive format: one of "zip", "tar", "gztar", "bztar", or |
| 610 | "xztar". Or any other format registered with |
| 611 | :func:`register_unpack_format`. If not provided, :func:`unpack_archive` |
| 612 | will use the archive file name extension and see if an unpacker was |
| 613 | registered for that extension. In case none is found, |
| 614 | a :exc:`ValueError` is raised. |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 615 | |
Jelle Zijlstra | a12df7b | 2017-05-05 14:27:12 -0700 | [diff] [blame] | 616 | .. versionchanged:: 3.7 |
| 617 | Accepts a :term:`path-like object` for *filename* and *extract_dir*. |
| 618 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 619 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 620 | .. function:: register_unpack_format(name, extensions, function[, extra_args[, description]]) |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 621 | |
| 622 | Registers an unpack format. *name* is the name of the format and |
| 623 | *extensions* is a list of extensions corresponding to the format, like |
| 624 | ``.zip`` for Zip files. |
| 625 | |
| 626 | *function* is the callable that will be used to unpack archives. The |
| 627 | callable will receive the path of the archive, followed by the directory |
| 628 | the archive must be extracted to. |
| 629 | |
| 630 | When provided, *extra_args* is a sequence of ``(name, value)`` tuples that |
| 631 | will be passed as keywords arguments to the callable. |
| 632 | |
| 633 | *description* can be provided to describe the format, and will be returned |
| 634 | by the :func:`get_unpack_formats` function. |
| 635 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 636 | |
| 637 | .. function:: unregister_unpack_format(name) |
| 638 | |
| 639 | Unregister an unpack format. *name* is the name of the format. |
| 640 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 641 | |
| 642 | .. function:: get_unpack_formats() |
| 643 | |
| 644 | Return a list of all registered formats for unpacking. |
| 645 | Each element of the returned sequence is a tuple |
| 646 | ``(name, extensions, description)``. |
| 647 | |
| 648 | By default :mod:`shutil` provides these formats: |
| 649 | |
Martin Panter | 2f9171d | 2016-12-18 01:23:09 +0000 | [diff] [blame] | 650 | - *zip*: ZIP file (unpacking compressed files works only if the corresponding |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 651 | module is available). |
| 652 | - *tar*: uncompressed tar file. |
| 653 | - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available). |
| 654 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available). |
| 655 | - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available). |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 656 | |
| 657 | You can register new formats or provide your own unpacker for any existing |
| 658 | formats, by using :func:`register_unpack_format`. |
| 659 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 660 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 661 | .. _shutil-archiving-example: |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 662 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 663 | Archiving example |
Georg Brandl | 03b9ad0 | 2012-06-24 18:09:40 +0200 | [diff] [blame] | 664 | ~~~~~~~~~~~~~~~~~ |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 665 | |
| 666 | In this example, we create a gzip'ed tar-file archive containing all files |
| 667 | found in the :file:`.ssh` directory of the user:: |
| 668 | |
| 669 | >>> from shutil import make_archive |
| 670 | >>> import os |
| 671 | >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) |
| 672 | >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh')) |
| 673 | >>> make_archive(archive_name, 'gztar', root_dir) |
| 674 | '/Users/tarek/myarchive.tar.gz' |
| 675 | |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 676 | The resulting archive contains: |
| 677 | |
| 678 | .. code-block:: shell-session |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 679 | |
| 680 | $ tar -tzvf /Users/tarek/myarchive.tar.gz |
| 681 | drwx------ tarek/staff 0 2010-02-01 16:23:40 ./ |
| 682 | -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys |
| 683 | -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config |
| 684 | -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa |
| 685 | -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub |
| 686 | -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa |
| 687 | -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub |
| 688 | -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 689 | |
| 690 | |
| 691 | Querying the size of the output terminal |
| 692 | ---------------------------------------- |
| 693 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 694 | .. function:: get_terminal_size(fallback=(columns, lines)) |
| 695 | |
| 696 | Get the size of the terminal window. |
| 697 | |
| 698 | For each of the two dimensions, the environment variable, ``COLUMNS`` |
| 699 | and ``LINES`` respectively, is checked. If the variable is defined and |
| 700 | the value is a positive integer, it is used. |
| 701 | |
| 702 | When ``COLUMNS`` or ``LINES`` is not defined, which is the common case, |
| 703 | the terminal connected to :data:`sys.__stdout__` is queried |
| 704 | by invoking :func:`os.get_terminal_size`. |
| 705 | |
| 706 | If the terminal size cannot be successfully queried, either because |
| 707 | the system doesn't support querying, or because we are not |
| 708 | connected to a terminal, the value given in ``fallback`` parameter |
| 709 | is used. ``fallback`` defaults to ``(80, 24)`` which is the default |
| 710 | size used by many terminal emulators. |
| 711 | |
| 712 | The value returned is a named tuple of type :class:`os.terminal_size`. |
| 713 | |
| 714 | See also: The Single UNIX Specification, Version 2, |
| 715 | `Other Environment Variables`_. |
| 716 | |
Berker Peksag | 8e2bdc8 | 2016-12-27 15:09:11 +0300 | [diff] [blame] | 717 | .. versionadded:: 3.3 |
| 718 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 719 | .. _`fcopyfile`: |
| 720 | http://www.manpagez.com/man/3/copyfile/ |
| 721 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 722 | .. _`Other Environment Variables`: |
| 723 | http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003 |