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 |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 54 | *dst* and return *dst*. *src* and *dst* are path names given as strings. |
| 55 | *dst* must be the complete target file name; look at :func:`shutil.copy` |
| 56 | 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] | 57 | specify the same file, :exc:`SameFileError` is raised. |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 58 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 59 | The destination location must be writable; otherwise, an :exc:`OSError` |
| 60 | exception will be raised. If *dst* already exists, it will be replaced. |
| 61 | Special files such as character or block devices and pipes cannot be |
| 62 | copied with this function. |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 63 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 64 | If *follow_symlinks* is false and *src* is a symbolic link, |
| 65 | a new symbolic link will be created instead of copying the |
| 66 | file *src* points to. |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 67 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 68 | .. versionchanged:: 3.3 |
| 69 | :exc:`IOError` used to be raised instead of :exc:`OSError`. |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 70 | Added *follow_symlinks* argument. |
| 71 | Now returns *dst*. |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 72 | |
Hynek Schlawack | 4865376 | 2012-10-07 12:49:58 +0200 | [diff] [blame] | 73 | .. versionchanged:: 3.4 |
Hynek Schlawack | 27ddb57 | 2012-10-28 13:59:27 +0100 | [diff] [blame] | 74 | Raise :exc:`SameFileError` instead of :exc:`Error`. Since the former is |
| 75 | a subclass of the latter, this change is backward compatible. |
Hynek Schlawack | 4865376 | 2012-10-07 12:49:58 +0200 | [diff] [blame] | 76 | |
| 77 | |
| 78 | .. exception:: SameFileError |
| 79 | |
| 80 | This exception is raised if source and destination in :func:`copyfile` |
| 81 | are the same file. |
| 82 | |
| 83 | .. versionadded:: 3.4 |
| 84 | |
| 85 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 86 | .. function:: copymode(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
| 88 | 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] | 89 | group are unaffected. *src* and *dst* are path names given as strings. |
| 90 | If *follow_symlinks* is false, and both *src* and *dst* are symbolic links, |
| 91 | :func:`copymode` will attempt to modify the mode of *dst* itself (rather |
| 92 | than the file it points to). This functionality is not available on every |
| 93 | platform; please see :func:`copystat` for more information. If |
| 94 | :func:`copymode` cannot modify symbolic links on the local platform, and it |
| 95 | is asked to do so, it will do nothing and return. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 97 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 98 | Added *follow_symlinks* argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 100 | .. function:: copystat(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 102 | Copy the permission bits, last access time, last modification time, and |
| 103 | flags from *src* to *dst*. On Linux, :func:`copystat` also copies the |
| 104 | "extended attributes" where possible. The file contents, owner, and |
| 105 | group are unaffected. *src* and *dst* are path names given as strings. |
| 106 | |
| 107 | If *follow_symlinks* is false, and *src* and *dst* both |
| 108 | refer to symbolic links, :func:`copystat` will operate on |
| 109 | the symbolic links themselves rather than the files the |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 110 | symbolic links refer to—reading the information from the |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 111 | *src* symbolic link, and writing the information to the |
| 112 | *dst* symbolic link. |
| 113 | |
| 114 | .. note:: |
| 115 | |
| 116 | Not all platforms provide the ability to examine and |
| 117 | modify symbolic links. Python itself can tell you what |
| 118 | functionality is locally available. |
| 119 | |
| 120 | * If ``os.chmod in os.supports_follow_symlinks`` is |
| 121 | ``True``, :func:`copystat` can modify the permission |
| 122 | bits of a symbolic link. |
| 123 | |
| 124 | * If ``os.utime in os.supports_follow_symlinks`` is |
| 125 | ``True``, :func:`copystat` can modify the last access |
| 126 | and modification times of a symbolic link. |
| 127 | |
| 128 | * If ``os.chflags in os.supports_follow_symlinks`` is |
| 129 | ``True``, :func:`copystat` can modify the flags of |
| 130 | a symbolic link. (``os.chflags`` is not available on |
| 131 | all platforms.) |
| 132 | |
| 133 | On platforms where some or all of this functionality |
| 134 | is unavailable, when asked to modify a symbolic link, |
| 135 | :func:`copystat` will copy everything it can. |
| 136 | :func:`copystat` never returns failure. |
| 137 | |
| 138 | Please see :data:`os.supports_follow_symlinks` |
| 139 | for more information. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 141 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 142 | Added *follow_symlinks* argument and support for Linux extended attributes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 144 | .. function:: copy(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 146 | Copies the file *src* to the file or directory *dst*. *src* and *dst* |
| 147 | should be strings. If *dst* specifies a directory, the file will be |
| 148 | copied into *dst* using the base filename from *src*. Returns the |
| 149 | path to the newly created file. |
| 150 | |
| 151 | If *follow_symlinks* is false, and *src* is a symbolic link, |
| 152 | *dst* will be created as a symbolic link. If *follow_symlinks* |
| 153 | is true and *src* is a symbolic link, *dst* will be a copy of |
| 154 | the file *src* refers to. |
| 155 | |
| 156 | :func:`copy` copies the file data and the file's permission |
| 157 | mode (see :func:`os.chmod`). Other metadata, like the |
| 158 | file's creation and modification times, is not preserved. |
| 159 | To preserve all file metadata from the original, use |
| 160 | :func:`~shutil.copy2` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 162 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 163 | Added *follow_symlinks* argument. |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 164 | Now returns path to the newly created file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 166 | .. function:: copy2(src, dst, *, follow_symlinks=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 168 | Identical to :func:`~shutil.copy` except that :func:`copy2` |
| 169 | also attempts to preserve all file metadata. |
| 170 | |
| 171 | When *follow_symlinks* is false, and *src* is a symbolic |
| 172 | link, :func:`copy2` attempts to copy all metadata from the |
| 173 | *src* symbolic link to the newly-created *dst* symbolic link. |
| 174 | However, this functionality is not available on all platforms. |
| 175 | On platforms where some or all of this functionality is |
| 176 | unavailable, :func:`copy2` will preserve all the metadata |
| 177 | it can; :func:`copy2` never returns failure. |
| 178 | |
| 179 | :func:`copy2` uses :func:`copystat` to copy the file metadata. |
| 180 | Please see :func:`copystat` for more information |
| 181 | about platform support for modifying symbolic link metadata. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 183 | .. versionchanged:: 3.3 |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 184 | Added *follow_symlinks* argument, try to copy extended |
| 185 | file system attributes too (currently Linux only). |
Larry Hastings | 60eba57 | 2012-09-21 10:12:14 -0700 | [diff] [blame] | 186 | Now returns path to the newly created file. |
Brian Curtin | 066dacf | 2012-06-19 10:03:05 -0500 | [diff] [blame] | 187 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 188 | .. function:: ignore_patterns(\*patterns) |
| 189 | |
| 190 | This factory function creates a function that can be used as a callable for |
| 191 | :func:`copytree`\'s *ignore* argument, ignoring files and directories that |
| 192 | match one of the glob-style *patterns* provided. See the example below. |
| 193 | |
| 194 | |
R David Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 195 | .. function:: copytree(src, dst, symlinks=False, ignore=None, \ |
| 196 | copy_function=copy2, ignore_dangling_symlinks=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | |
Brian Curtin | 0d0a1de | 2012-06-18 18:41:07 -0500 | [diff] [blame] | 198 | Recursively copy an entire directory tree rooted at *src*, returning the |
| 199 | destination directory. The destination |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 200 | directory, named by *dst*, must not already exist; it will be created as |
| 201 | well as missing parent directories. Permissions and times of directories |
| 202 | are copied with :func:`copystat`, individual files are copied using |
| 203 | :func:`shutil.copy2`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 205 | 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] | 206 | symbolic links in the new tree and the metadata of the original links will |
| 207 | be copied as far as the platform allows; if false or omitted, the contents |
| 208 | and metadata of the linked files are copied to the new tree. |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 209 | |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 210 | 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] | 211 | exist, an exception will be added in the list of errors raised in |
| 212 | an :exc:`Error` exception at the end of the copy process. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 213 | 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] | 214 | want to silence this exception. Notice that this option has no effect |
| 215 | on platforms that don't support :func:`os.symlink`. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 216 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 217 | If *ignore* is given, it must be a callable that will receive as its |
| 218 | arguments the directory being visited by :func:`copytree`, and a list of its |
| 219 | contents, as returned by :func:`os.listdir`. Since :func:`copytree` is |
| 220 | called recursively, the *ignore* callable will be called once for each |
| 221 | directory that is copied. The callable must return a sequence of directory |
| 222 | and file names relative to the current directory (i.e. a subset of the items |
| 223 | in its second argument); these names will then be ignored in the copy |
| 224 | process. :func:`ignore_patterns` can be used to create such a callable that |
| 225 | ignores names based on glob-style patterns. |
| 226 | |
| 227 | If exception(s) occur, an :exc:`Error` is raised with a list of reasons. |
| 228 | |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 229 | If *copy_function* is given, it must be a callable that will be used to copy |
| 230 | each file. It will be called with the source path and the destination path |
| 231 | as arguments. By default, :func:`shutil.copy2` is used, but any function |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 232 | that supports the same signature (like :func:`shutil.copy`) can be used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 234 | .. versionchanged:: 3.3 |
| 235 | Copy metadata when *symlinks* is false. |
| 236 | Now returns *dst*. |
| 237 | |
Tarek Ziadé | 5340db3 | 2010-04-19 22:30:51 +0000 | [diff] [blame] | 238 | .. versionchanged:: 3.2 |
| 239 | Added the *copy_function* argument to be able to provide a custom copy |
| 240 | function. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 241 | Added the *ignore_dangling_symlinks* argument to silent dangling symlinks |
| 242 | errors when *symlinks* is false. |
| 243 | |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 244 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 245 | .. function:: rmtree(path, ignore_errors=False, onerror=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
| 247 | .. index:: single: directory; deleting |
| 248 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 249 | Delete an entire directory tree; *path* must point to a directory (but not a |
| 250 | symbolic link to a directory). If *ignore_errors* is true, errors resulting |
| 251 | from failed removals will be ignored; if false or omitted, such errors are |
| 252 | handled by calling a handler specified by *onerror* or, if that is omitted, |
| 253 | they raise an exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 255 | .. note:: |
Hynek Schlawack | 67be92b | 2012-06-23 17:58:42 +0200 | [diff] [blame] | 256 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 257 | On platforms that support the necessary fd-based functions a symlink |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 258 | attack resistant version of :func:`rmtree` is used by default. On other |
| 259 | platforms, the :func:`rmtree` implementation is susceptible to a symlink |
| 260 | attack: given proper timing and circumstances, attackers can manipulate |
| 261 | symlinks on the filesystem to delete files they wouldn't be able to access |
| 262 | otherwise. Applications can use the :data:`rmtree.avoids_symlink_attacks` |
| 263 | function attribute to determine which case applies. |
Hynek Schlawack | 67be92b | 2012-06-23 17:58:42 +0200 | [diff] [blame] | 264 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 265 | If *onerror* is provided, it must be a callable that accepts three |
Hynek Schlawack | 67be92b | 2012-06-23 17:58:42 +0200 | [diff] [blame] | 266 | parameters: *function*, *path*, and *excinfo*. |
| 267 | |
| 268 | The first parameter, *function*, is the function which raised the exception; |
| 269 | it depends on the platform and implementation. The second parameter, |
| 270 | *path*, will be the path name passed to *function*. The third parameter, |
| 271 | *excinfo*, will be the exception information returned by |
| 272 | :func:`sys.exc_info`. Exceptions raised by *onerror* will not be caught. |
| 273 | |
| 274 | .. versionchanged:: 3.3 |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 275 | Added a symlink attack resistant version that is used automatically |
| 276 | if platform supports fd-based functions. |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 277 | |
Éric Araujo | 544e13d | 2012-06-24 13:53:48 -0400 | [diff] [blame] | 278 | .. attribute:: rmtree.avoids_symlink_attacks |
Hynek Schlawack | 2100b42 | 2012-06-23 20:28:32 +0200 | [diff] [blame] | 279 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 280 | Indicates whether the current platform and implementation provides a |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 281 | symlink attack resistant version of :func:`rmtree`. Currently this is |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 282 | only true for platforms supporting fd-based directory access functions. |
Hynek Schlawack | 2100b42 | 2012-06-23 20:28:32 +0200 | [diff] [blame] | 283 | |
Nick Coghlan | 5b0eca1 | 2012-06-24 16:43:06 +1000 | [diff] [blame] | 284 | .. versionadded:: 3.3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 | |
Georg Brandl | 96acb73 | 2012-06-24 17:39:05 +0200 | [diff] [blame] | 286 | |
R David Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 287 | .. function:: move(src, dst, copy_function=copy2) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | |
Brian Curtin | 0d0a1de | 2012-06-18 18:41:07 -0500 | [diff] [blame] | 289 | Recursively move a file or directory (*src*) to another location (*dst*) |
| 290 | and return the destination. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | |
Benjamin Peterson | 218144a | 2015-03-22 10:11:54 -0400 | [diff] [blame] | 292 | If the destination is an existing directory, then *src* is moved inside that |
| 293 | directory. If the destination already exists but is not a directory, it may |
| 294 | be overwritten depending on :func:`os.rename` semantics. |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 295 | |
| 296 | 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] | 297 | used. Otherwise, *src* is copied to *dst* using *copy_function* and then |
| 298 | removed. In case of symlinks, a new symlink pointing to the target of *src* |
| 299 | will be created in or as *dst* and *src* will be removed. |
| 300 | |
| 301 | If *copy_function* is given, it must be a callable that takes two arguments |
| 302 | *src* and *dst*, and will be used to copy *src* to *dest* if |
| 303 | :func:`os.rename` cannot be used. If the source is a directory, |
| 304 | :func:`copytree` is called, passing it the :func:`copy_function`. The |
| 305 | default *copy_function* is :func:`copy2`. Using :func:`copy` as the |
| 306 | *copy_function* allows the move to succeed when it is not possible to also |
| 307 | 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] | 308 | |
| 309 | .. versionchanged:: 3.3 |
| 310 | Added explicit symlink handling for foreign filesystems, thus adapting |
| 311 | it to the behavior of GNU's :program:`mv`. |
Larry Hastings | 7aa2c8b | 2012-07-15 16:58:29 -0700 | [diff] [blame] | 312 | Now returns *dst*. |
Brian Curtin | 066dacf | 2012-06-19 10:03:05 -0500 | [diff] [blame] | 313 | |
R David Murray | 6ffface | 2014-06-11 14:40:13 -0400 | [diff] [blame] | 314 | .. versionchanged:: 3.5 |
| 315 | Added the *copy_function* keyword argument. |
| 316 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 317 | .. function:: disk_usage(path) |
| 318 | |
Éric Araujo | e4d5b8e | 2011-08-08 16:51:11 +0200 | [diff] [blame] | 319 | Return disk usage statistics about the given path as a :term:`named tuple` |
| 320 | with the attributes *total*, *used* and *free*, which are the amount of |
| 321 | total, used and free space, in bytes. |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 322 | |
| 323 | .. versionadded:: 3.3 |
| 324 | |
| 325 | Availability: Unix, Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 326 | |
Sandro Tosi | d902a14 | 2011-08-22 23:28:27 +0200 | [diff] [blame] | 327 | .. function:: chown(path, user=None, group=None) |
| 328 | |
| 329 | Change owner *user* and/or *group* of the given *path*. |
| 330 | |
| 331 | *user* can be a system user name or a uid; the same applies to *group*. At |
| 332 | least one argument is required. |
| 333 | |
| 334 | See also :func:`os.chown`, the underlying function. |
| 335 | |
| 336 | Availability: Unix. |
| 337 | |
| 338 | .. versionadded:: 3.3 |
| 339 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 340 | |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 341 | .. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None) |
| 342 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 343 | Return the path to an executable which would be run if the given *cmd* was |
| 344 | called. If no *cmd* would be called, return ``None``. |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 345 | |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 346 | *mode* is a permission mask passed to :func:`os.access`, by default |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 347 | determining if the file exists and executable. |
| 348 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 349 | When no *path* is specified, the results of :func:`os.environ` are used, |
| 350 | returning either the "PATH" value or a fallback of :attr:`os.defpath`. |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 351 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 352 | On Windows, the current directory is always prepended to the *path* whether |
| 353 | 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] | 354 | command shell uses when finding executables. Additionally, when finding the |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 355 | *cmd* in the *path*, the ``PATHEXT`` environment variable is checked. For |
| 356 | example, if you call ``shutil.which("python")``, :func:`which` will search |
| 357 | ``PATHEXT`` to know that it should look for ``python.exe`` within the *path* |
| 358 | directories. For example, on Windows:: |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 359 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 360 | >>> shutil.which("python") |
Serhiy Storchaka | 80c88f4 | 2013-01-22 10:31:36 +0200 | [diff] [blame] | 361 | 'C:\\Python33\\python.EXE' |
Brian Curtin | c57a345 | 2012-06-22 16:00:30 -0500 | [diff] [blame] | 362 | |
| 363 | .. versionadded:: 3.3 |
Sandro Tosi | d902a14 | 2011-08-22 23:28:27 +0200 | [diff] [blame] | 364 | |
Georg Brandl | 4a7e25f | 2012-06-24 17:37:07 +0200 | [diff] [blame] | 365 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 366 | .. exception:: Error |
| 367 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 368 | This exception collects exceptions that are raised during a multi-file |
| 369 | operation. For :func:`copytree`, the exception argument is a list of 3-tuples |
| 370 | (*srcname*, *dstname*, *exception*). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 371 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 372 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 373 | .. _shutil-copytree-example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 374 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 375 | copytree example |
Georg Brandl | 03b9ad0 | 2012-06-24 18:09:40 +0200 | [diff] [blame] | 376 | ~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 377 | |
| 378 | This example is the implementation of the :func:`copytree` function, described |
| 379 | above, with the docstring omitted. It demonstrates many of the other functions |
| 380 | provided by this module. :: |
| 381 | |
| 382 | def copytree(src, dst, symlinks=False): |
| 383 | names = os.listdir(src) |
| 384 | os.makedirs(dst) |
| 385 | errors = [] |
| 386 | for name in names: |
| 387 | srcname = os.path.join(src, name) |
| 388 | dstname = os.path.join(dst, name) |
| 389 | try: |
| 390 | if symlinks and os.path.islink(srcname): |
| 391 | linkto = os.readlink(srcname) |
| 392 | os.symlink(linkto, dstname) |
| 393 | elif os.path.isdir(srcname): |
| 394 | copytree(srcname, dstname, symlinks) |
| 395 | else: |
| 396 | copy2(srcname, dstname) |
| 397 | # XXX What about devices, sockets etc.? |
Andrew Svetlov | 618c2e1 | 2012-12-15 22:59:24 +0200 | [diff] [blame] | 398 | except OSError as why: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 399 | errors.append((srcname, dstname, str(why))) |
| 400 | # catch the Error from the recursive copytree so that we can |
| 401 | # continue with other files |
| 402 | except Error as err: |
| 403 | errors.extend(err.args[0]) |
| 404 | try: |
| 405 | copystat(src, dst) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 406 | except OSError as why: |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 407 | # can't copy file access times on Windows |
| 408 | if why.winerror is None: |
| 409 | errors.extend((src, dst, str(why))) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | if errors: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 411 | raise Error(errors) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 412 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 413 | Another example that uses the :func:`ignore_patterns` helper:: |
| 414 | |
| 415 | from shutil import copytree, ignore_patterns |
| 416 | |
| 417 | copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*')) |
| 418 | |
| 419 | This will copy everything except ``.pyc`` files and files or directories whose |
| 420 | name starts with ``tmp``. |
| 421 | |
| 422 | Another example that uses the *ignore* argument to add a logging call:: |
| 423 | |
| 424 | from shutil import copytree |
| 425 | import logging |
| 426 | |
| 427 | def _logpath(path, names): |
Vinay Sajip | dd917f8 | 2016-08-31 08:22:29 +0100 | [diff] [blame] | 428 | logging.info('Working in %s', path) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 429 | return [] # nothing will be ignored |
| 430 | |
| 431 | copytree(source, destination, ignore=_logpath) |
| 432 | |
| 433 | |
Tim Golden | 7833779 | 2014-05-07 18:05:45 +0100 | [diff] [blame] | 434 | .. _shutil-rmtree-example: |
| 435 | |
| 436 | rmtree example |
| 437 | ~~~~~~~~~~~~~~ |
| 438 | |
| 439 | This example shows how to remove a directory tree on Windows where some |
| 440 | of the files have their read-only bit set. It uses the onerror callback |
| 441 | to clear the readonly bit and reattempt the remove. Any subsequent failure |
| 442 | will propagate. :: |
| 443 | |
| 444 | import os, stat |
| 445 | import shutil |
Tim Golden | ba74885 | 2014-05-07 18:08:08 +0100 | [diff] [blame] | 446 | |
Tim Golden | 7833779 | 2014-05-07 18:05:45 +0100 | [diff] [blame] | 447 | def remove_readonly(func, path, _): |
| 448 | "Clear the readonly bit and reattempt the removal" |
| 449 | os.chmod(path, stat.S_IWRITE) |
Tim Golden | ba74885 | 2014-05-07 18:08:08 +0100 | [diff] [blame] | 450 | func(path) |
| 451 | |
Tim Golden | 7833779 | 2014-05-07 18:05:45 +0100 | [diff] [blame] | 452 | shutil.rmtree(directory, onerror=remove_readonly) |
| 453 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 454 | .. _archiving-operations: |
| 455 | |
| 456 | Archiving operations |
| 457 | -------------------- |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 458 | |
Georg Brandl | 03b9ad0 | 2012-06-24 18:09:40 +0200 | [diff] [blame] | 459 | .. versionadded:: 3.2 |
| 460 | |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 461 | .. versionchanged:: 3.5 |
| 462 | Added support for the *xztar* format. |
| 463 | |
| 464 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 465 | High-level utilities to create and read compressed and archived files are also |
| 466 | provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. |
| 467 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 468 | .. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]]) |
| 469 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 470 | 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] | 471 | |
| 472 | *base_name* is the name of the file to create, including the path, minus |
| 473 | any format-specific extension. *format* is the archive format: one of |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 474 | "zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the |
| 475 | :mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is |
| 476 | available), or "xztar" (if the :mod:`lzma` module is available). |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 477 | |
| 478 | *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] | 479 | archive; for example, we typically chdir into *root_dir* before creating the |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 480 | archive. |
| 481 | |
| 482 | *base_dir* is the directory where we start archiving from; |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 483 | 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] | 484 | directories in the archive. |
| 485 | |
| 486 | *root_dir* and *base_dir* both default to the current directory. |
| 487 | |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 488 | If *dry_run* is true, no archive is created, but the operations that would be |
| 489 | executed are logged to *logger*. |
| 490 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 491 | *owner* and *group* are used when creating a tar archive. By default, |
| 492 | uses the current owner and group. |
| 493 | |
Éric Araujo | 06c42a3 | 2011-11-07 17:31:07 +0100 | [diff] [blame] | 494 | *logger* must be an object compatible with :pep:`282`, usually an instance of |
| 495 | :class:`logging.Logger`. |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 496 | |
Georg Brandl | 36ac510 | 2014-10-31 10:54:06 +0100 | [diff] [blame] | 497 | The *verbose* argument is unused and deprecated. |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 498 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 499 | |
| 500 | .. function:: get_archive_formats() |
| 501 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 502 | Return a list of supported formats for archiving. |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 503 | Each element of the returned sequence is a tuple ``(name, description)``. |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 504 | |
| 505 | By default :mod:`shutil` provides these formats: |
| 506 | |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 507 | - *zip*: ZIP file (if the :mod:`zlib` module is available). |
| 508 | - *tar*: uncompressed tar file. |
| 509 | - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available). |
| 510 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available). |
| 511 | - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available). |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 512 | |
| 513 | You can register new formats or provide your own archiver for any existing |
| 514 | formats, by using :func:`register_archive_format`. |
| 515 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 516 | |
| 517 | .. function:: register_archive_format(name, function, [extra_args, [description]]) |
| 518 | |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 519 | Register an archiver for the format *name*. |
| 520 | |
| 521 | *function* is the callable that will be used to unpack archives. The callable |
| 522 | will receive the *base_name* of the file to create, followed by the |
| 523 | *base_dir* (which defaults to :data:`os.curdir`) to start archiving from. |
| 524 | Further arguments are passed as keyword arguments: *owner*, *group*, |
| 525 | *dry_run* and *logger* (as passed in :func:`make_archive`). |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 526 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 527 | 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] | 528 | used as extra keywords arguments when the archiver callable is used. |
| 529 | |
| 530 | *description* is used by :func:`get_archive_formats` which returns the |
Georg Brandl | 9b1b0e5 | 2014-10-31 10:02:40 +0100 | [diff] [blame] | 531 | list of archivers. Defaults to an empty string. |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 532 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 533 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 534 | .. function:: unregister_archive_format(name) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 535 | |
| 536 | Remove the archive format *name* from the list of supported formats. |
| 537 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 538 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 539 | .. function:: unpack_archive(filename[, extract_dir[, format]]) |
| 540 | |
| 541 | Unpack an archive. *filename* is the full path of the archive. |
| 542 | |
| 543 | *extract_dir* is the name of the target directory where the archive is |
| 544 | unpacked. If not provided, the current working directory is used. |
| 545 | |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 546 | *format* is the archive format: one of "zip", "tar", "gztar", "bztar", or |
| 547 | "xztar". Or any other format registered with |
| 548 | :func:`register_unpack_format`. If not provided, :func:`unpack_archive` |
| 549 | will use the archive file name extension and see if an unpacker was |
| 550 | registered for that extension. In case none is found, |
| 551 | a :exc:`ValueError` is raised. |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 552 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 553 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 554 | .. function:: register_unpack_format(name, extensions, function[, extra_args[, description]]) |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 555 | |
| 556 | Registers an unpack format. *name* is the name of the format and |
| 557 | *extensions* is a list of extensions corresponding to the format, like |
| 558 | ``.zip`` for Zip files. |
| 559 | |
| 560 | *function* is the callable that will be used to unpack archives. The |
| 561 | callable will receive the path of the archive, followed by the directory |
| 562 | the archive must be extracted to. |
| 563 | |
| 564 | When provided, *extra_args* is a sequence of ``(name, value)`` tuples that |
| 565 | will be passed as keywords arguments to the callable. |
| 566 | |
| 567 | *description* can be provided to describe the format, and will be returned |
| 568 | by the :func:`get_unpack_formats` function. |
| 569 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 570 | |
| 571 | .. function:: unregister_unpack_format(name) |
| 572 | |
| 573 | Unregister an unpack format. *name* is the name of the format. |
| 574 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 575 | |
| 576 | .. function:: get_unpack_formats() |
| 577 | |
| 578 | Return a list of all registered formats for unpacking. |
| 579 | Each element of the returned sequence is a tuple |
| 580 | ``(name, extensions, description)``. |
| 581 | |
| 582 | By default :mod:`shutil` provides these formats: |
| 583 | |
Martin Panter | 2f9171d | 2016-12-18 01:23:09 +0000 | [diff] [blame] | 584 | - *zip*: ZIP file (unpacking compressed files works only if the corresponding |
Serhiy Storchaka | 20cdffd | 2016-12-16 18:58:33 +0200 | [diff] [blame] | 585 | module is available). |
| 586 | - *tar*: uncompressed tar file. |
| 587 | - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available). |
| 588 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available). |
| 589 | - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available). |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 590 | |
| 591 | You can register new formats or provide your own unpacker for any existing |
| 592 | formats, by using :func:`register_unpack_format`. |
| 593 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 594 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 595 | .. _shutil-archiving-example: |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 596 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 597 | Archiving example |
Georg Brandl | 03b9ad0 | 2012-06-24 18:09:40 +0200 | [diff] [blame] | 598 | ~~~~~~~~~~~~~~~~~ |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 599 | |
| 600 | In this example, we create a gzip'ed tar-file archive containing all files |
| 601 | found in the :file:`.ssh` directory of the user:: |
| 602 | |
| 603 | >>> from shutil import make_archive |
| 604 | >>> import os |
| 605 | >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) |
| 606 | >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh')) |
| 607 | >>> make_archive(archive_name, 'gztar', root_dir) |
| 608 | '/Users/tarek/myarchive.tar.gz' |
| 609 | |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 610 | The resulting archive contains: |
| 611 | |
| 612 | .. code-block:: shell-session |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 613 | |
| 614 | $ tar -tzvf /Users/tarek/myarchive.tar.gz |
| 615 | drwx------ tarek/staff 0 2010-02-01 16:23:40 ./ |
| 616 | -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys |
| 617 | -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config |
| 618 | -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa |
| 619 | -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub |
| 620 | -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa |
| 621 | -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub |
| 622 | -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] | 623 | |
| 624 | |
| 625 | Querying the size of the output terminal |
| 626 | ---------------------------------------- |
| 627 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 628 | .. function:: get_terminal_size(fallback=(columns, lines)) |
| 629 | |
| 630 | Get the size of the terminal window. |
| 631 | |
| 632 | For each of the two dimensions, the environment variable, ``COLUMNS`` |
| 633 | and ``LINES`` respectively, is checked. If the variable is defined and |
| 634 | the value is a positive integer, it is used. |
| 635 | |
| 636 | When ``COLUMNS`` or ``LINES`` is not defined, which is the common case, |
| 637 | the terminal connected to :data:`sys.__stdout__` is queried |
| 638 | by invoking :func:`os.get_terminal_size`. |
| 639 | |
| 640 | If the terminal size cannot be successfully queried, either because |
| 641 | the system doesn't support querying, or because we are not |
| 642 | connected to a terminal, the value given in ``fallback`` parameter |
| 643 | is used. ``fallback`` defaults to ``(80, 24)`` which is the default |
| 644 | size used by many terminal emulators. |
| 645 | |
| 646 | The value returned is a named tuple of type :class:`os.terminal_size`. |
| 647 | |
| 648 | See also: The Single UNIX Specification, Version 2, |
| 649 | `Other Environment Variables`_. |
| 650 | |
Berker Peksag | 8e2bdc8 | 2016-12-27 15:09:11 +0300 | [diff] [blame] | 651 | .. versionadded:: 3.3 |
| 652 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 653 | .. _`Other Environment Variables`: |
| 654 | http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003 |
| 655 | |