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