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 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 50 | .. function:: copyfile(src, dst[, symlinks=False]) |
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 |
| 53 | *dst*. *dst* must be the complete target file name; look at |
| 54 | :func:`shutil.copy` for a copy that accepts a target directory path. If |
| 55 | *src* and *dst* are the same files, :exc:`Error` is raised. |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 56 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 57 | The destination location must be writable; otherwise, an :exc:`OSError` exception |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 58 | will be raised. If *dst* already exists, it will be replaced. Special files |
| 59 | such as character or block devices and pipes cannot be copied with this |
| 60 | function. *src* and *dst* are path names given as strings. |
| 61 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 62 | If *symlinks* is true and *src* is a symbolic link, a new symbolic link will |
| 63 | be created instead of copying the file *src* points to. |
| 64 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 65 | .. versionchanged:: 3.3 |
| 66 | :exc:`IOError` used to be raised instead of :exc:`OSError`. |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 67 | Added *symlinks* argument. |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 68 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 70 | .. function:: copymode(src, dst[, symlinks=False]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 72 | Copy the permission bits from *src* to *dst*. The file contents, owner, and |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 73 | group are unaffected. *src* and *dst* are path names given as strings. If |
| 74 | *symlinks* is true, *src* a symbolic link and the operating system supports |
| 75 | modes for symbolic links (for example BSD-based ones), the mode of the link |
| 76 | will be copied. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 78 | .. versionchanged:: 3.3 |
| 79 | Added *symlinks* argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 81 | .. function:: copystat(src, dst[, symlinks=False]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
| 83 | Copy the permission bits, last access time, last modification time, and flags |
| 84 | from *src* to *dst*. The file contents, owner, and group are unaffected. *src* |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 85 | and *dst* are path names given as strings. If *src* and *dst* are both |
| 86 | symbolic links and *symlinks* true, the stats of the link will be copied as |
| 87 | far as the platform allows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 89 | .. versionchanged:: 3.3 |
| 90 | Added *symlinks* argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 92 | .. function:: copy(src, dst[, symlinks=False])) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a |
| 95 | file with the same basename as *src* is created (or overwritten) in the |
| 96 | directory specified. Permission bits are copied. *src* and *dst* are path |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 97 | names given as strings. If *symlinks* is true, symbolic links won't be |
| 98 | followed but recreated instead -- this resembles GNU's :program:`cp -P`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 100 | .. versionchanged:: 3.3 |
| 101 | Added *symlinks* argument. |
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 | .. function:: copy2(src, dst[, symlinks=False]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 105 | Similar to :func:`shutil.copy`, but metadata is copied as well -- in fact, |
| 106 | this is just :func:`shutil.copy` followed by :func:`copystat`. This is |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 107 | similar to the Unix command :program:`cp -p`. If *symlinks* is true, |
| 108 | symbolic links won't be followed but recreated instead -- this resembles |
| 109 | GNU's :program:`cp -P`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 111 | .. versionchanged:: 3.3 |
| 112 | Added *symlinks* argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 114 | .. function:: ignore_patterns(\*patterns) |
| 115 | |
| 116 | This factory function creates a function that can be used as a callable for |
| 117 | :func:`copytree`\'s *ignore* argument, ignoring files and directories that |
| 118 | match one of the glob-style *patterns* provided. See the example below. |
| 119 | |
| 120 | |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 121 | .. 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] | 122 | |
| 123 | Recursively copy an entire directory tree rooted at *src*. The destination |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 124 | directory, named by *dst*, must not already exist; it will be created as |
| 125 | well as missing parent directories. Permissions and times of directories |
| 126 | are copied with :func:`copystat`, individual files are copied using |
| 127 | :func:`shutil.copy2`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 129 | 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] | 130 | symbolic links in the new tree and the metadata of the original links will |
| 131 | be copied as far as the platform allows; if false or omitted, the contents |
| 132 | and metadata of the linked files are copied to the new tree. |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 133 | |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 134 | When *symlinks* is false, if the file pointed by the symlink doesn't |
| 135 | exist, a exception will be added in the list of errors raised in |
| 136 | a :exc:`Error` exception at the end of the copy process. |
| 137 | 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] | 138 | want to silence this exception. Notice that this option has no effect |
| 139 | on platforms that don't support :func:`os.symlink`. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 140 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 141 | If *ignore* is given, it must be a callable that will receive as its |
| 142 | arguments the directory being visited by :func:`copytree`, and a list of its |
| 143 | contents, as returned by :func:`os.listdir`. Since :func:`copytree` is |
| 144 | called recursively, the *ignore* callable will be called once for each |
| 145 | directory that is copied. The callable must return a sequence of directory |
| 146 | and file names relative to the current directory (i.e. a subset of the items |
| 147 | in its second argument); these names will then be ignored in the copy |
| 148 | process. :func:`ignore_patterns` can be used to create such a callable that |
| 149 | ignores names based on glob-style patterns. |
| 150 | |
| 151 | If exception(s) occur, an :exc:`Error` is raised with a list of reasons. |
| 152 | |
Senthil Kumaran | 7f728c1 | 2012-02-13 23:30:47 +0800 | [diff] [blame] | 153 | If *copy_function* is given, it must be a callable that will be used to copy |
| 154 | each file. It will be called with the source path and the destination path |
| 155 | as arguments. By default, :func:`shutil.copy2` is used, but any function |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 156 | that supports the same signature (like :func:`shutil.copy`) can be used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
Tarek Ziadé | 5340db3 | 2010-04-19 22:30:51 +0000 | [diff] [blame] | 158 | .. versionchanged:: 3.2 |
| 159 | Added the *copy_function* argument to be able to provide a custom copy |
| 160 | function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 162 | .. versionchanged:: 3.2 |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 163 | Added the *ignore_dangling_symlinks* argument to silent dangling symlinks |
| 164 | errors when *symlinks* is false. |
| 165 | |
Antoine Pitrou | 78091e6 | 2011-12-29 18:54:15 +0100 | [diff] [blame] | 166 | .. versionchanged:: 3.3 |
| 167 | Copy metadata when *symlinks* is false. |
| 168 | |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 169 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 170 | .. function:: rmtree(path, ignore_errors=False, onerror=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 | |
| 172 | .. index:: single: directory; deleting |
| 173 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 174 | Delete an entire directory tree; *path* must point to a directory (but not a |
| 175 | symbolic link to a directory). If *ignore_errors* is true, errors resulting |
| 176 | from failed removals will be ignored; if false or omitted, such errors are |
| 177 | handled by calling a handler specified by *onerror* or, if that is omitted, |
| 178 | they raise an exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 180 | If *onerror* is provided, it must be a callable that accepts three |
| 181 | parameters: *function*, *path*, and *excinfo*. The first parameter, |
| 182 | *function*, is the function which raised the exception; it will be |
| 183 | :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or |
| 184 | :func:`os.rmdir`. The second parameter, *path*, will be the path name passed |
| 185 | to *function*. The third parameter, *excinfo*, will be the exception |
| 186 | information return by :func:`sys.exc_info`. Exceptions raised by *onerror* |
| 187 | will not be caught. |
| 188 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
| 190 | .. function:: move(src, dst) |
| 191 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 192 | Recursively move a file or directory (*src*) to another location (*dst*). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 194 | If the destination is a directory or a symlink to a directory, then *src* is |
| 195 | moved inside that directory. |
| 196 | |
| 197 | The destination directory must not already exist. If the destination already |
| 198 | exists but is not a directory, it may be overwritten depending on |
| 199 | :func:`os.rename` semantics. |
| 200 | |
| 201 | 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] | 202 | used. Otherwise, *src* is copied (using :func:`shutil.copy2`) to *dst* and |
Senthil Kumaran | 1fd6482 | 2012-02-13 23:35:44 +0800 | [diff] [blame] | 203 | then removed. In case of symlinks, a new symlink pointing to the target of |
| 204 | *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] | 205 | |
| 206 | .. versionchanged:: 3.3 |
| 207 | Added explicit symlink handling for foreign filesystems, thus adapting |
| 208 | it to the behavior of GNU's :program:`mv`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 210 | .. function:: disk_usage(path) |
| 211 | |
Éric Araujo | e4d5b8e | 2011-08-08 16:51:11 +0200 | [diff] [blame] | 212 | Return disk usage statistics about the given path as a :term:`named tuple` |
| 213 | with the attributes *total*, *used* and *free*, which are the amount of |
| 214 | total, used and free space, in bytes. |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 215 | |
| 216 | .. versionadded:: 3.3 |
| 217 | |
| 218 | Availability: Unix, Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 | |
Sandro Tosi | d902a14 | 2011-08-22 23:28:27 +0200 | [diff] [blame] | 220 | .. function:: chown(path, user=None, group=None) |
| 221 | |
| 222 | Change owner *user* and/or *group* of the given *path*. |
| 223 | |
| 224 | *user* can be a system user name or a uid; the same applies to *group*. At |
| 225 | least one argument is required. |
| 226 | |
| 227 | See also :func:`os.chown`, the underlying function. |
| 228 | |
| 229 | Availability: Unix. |
| 230 | |
| 231 | .. versionadded:: 3.3 |
| 232 | |
| 233 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | .. exception:: Error |
| 235 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 236 | This exception collects exceptions that are raised during a multi-file |
| 237 | operation. For :func:`copytree`, the exception argument is a list of 3-tuples |
| 238 | (*srcname*, *dstname*, *exception*). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 241 | .. _shutil-copytree-example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 243 | copytree example |
| 244 | :::::::::::::::: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 | |
| 246 | This example is the implementation of the :func:`copytree` function, described |
| 247 | above, with the docstring omitted. It demonstrates many of the other functions |
| 248 | provided by this module. :: |
| 249 | |
| 250 | def copytree(src, dst, symlinks=False): |
| 251 | names = os.listdir(src) |
| 252 | os.makedirs(dst) |
| 253 | errors = [] |
| 254 | for name in names: |
| 255 | srcname = os.path.join(src, name) |
| 256 | dstname = os.path.join(dst, name) |
| 257 | try: |
| 258 | if symlinks and os.path.islink(srcname): |
| 259 | linkto = os.readlink(srcname) |
| 260 | os.symlink(linkto, dstname) |
| 261 | elif os.path.isdir(srcname): |
| 262 | copytree(srcname, dstname, symlinks) |
| 263 | else: |
| 264 | copy2(srcname, dstname) |
| 265 | # XXX What about devices, sockets etc.? |
| 266 | except (IOError, os.error) as why: |
| 267 | errors.append((srcname, dstname, str(why))) |
| 268 | # catch the Error from the recursive copytree so that we can |
| 269 | # continue with other files |
| 270 | except Error as err: |
| 271 | errors.extend(err.args[0]) |
| 272 | try: |
| 273 | copystat(src, dst) |
| 274 | except WindowsError: |
| 275 | # can't copy file access times on Windows |
| 276 | pass |
| 277 | except OSError as why: |
| 278 | errors.extend((src, dst, str(why))) |
| 279 | if errors: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 280 | raise Error(errors) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 281 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 282 | Another example that uses the :func:`ignore_patterns` helper:: |
| 283 | |
| 284 | from shutil import copytree, ignore_patterns |
| 285 | |
| 286 | copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*')) |
| 287 | |
| 288 | This will copy everything except ``.pyc`` files and files or directories whose |
| 289 | name starts with ``tmp``. |
| 290 | |
| 291 | Another example that uses the *ignore* argument to add a logging call:: |
| 292 | |
| 293 | from shutil import copytree |
| 294 | import logging |
| 295 | |
| 296 | def _logpath(path, names): |
| 297 | logging.info('Working in %s' % path) |
| 298 | return [] # nothing will be ignored |
| 299 | |
| 300 | copytree(source, destination, ignore=_logpath) |
| 301 | |
| 302 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 303 | .. _archiving-operations: |
| 304 | |
| 305 | Archiving operations |
| 306 | -------------------- |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 307 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 308 | High-level utilities to create and read compressed and archived files are also |
| 309 | provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. |
| 310 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 311 | .. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]]) |
| 312 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 313 | 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] | 314 | |
| 315 | *base_name* is the name of the file to create, including the path, minus |
| 316 | any format-specific extension. *format* is the archive format: one of |
Tarek Ziadé | ffa155a | 2010-04-29 13:34:35 +0000 | [diff] [blame] | 317 | "zip", "tar", "bztar" (if the :mod:`bz2` module is available) or "gztar". |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 318 | |
| 319 | *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] | 320 | archive; for example, we typically chdir into *root_dir* before creating the |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 321 | archive. |
| 322 | |
| 323 | *base_dir* is the directory where we start archiving from; |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 324 | 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] | 325 | directories in the archive. |
| 326 | |
| 327 | *root_dir* and *base_dir* both default to the current directory. |
| 328 | |
| 329 | *owner* and *group* are used when creating a tar archive. By default, |
| 330 | uses the current owner and group. |
| 331 | |
Éric Araujo | 06c42a3 | 2011-11-07 17:31:07 +0100 | [diff] [blame] | 332 | *logger* must be an object compatible with :pep:`282`, usually an instance of |
| 333 | :class:`logging.Logger`. |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 334 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 335 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | .. function:: get_archive_formats() |
| 339 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 340 | Return a list of supported formats for archiving. |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 341 | Each element of the returned sequence is a tuple ``(name, description)`` |
| 342 | |
| 343 | By default :mod:`shutil` provides these formats: |
| 344 | |
| 345 | - *gztar*: gzip'ed tar-file |
Tarek Ziadé | ffa155a | 2010-04-29 13:34:35 +0000 | [diff] [blame] | 346 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 347 | - *tar*: uncompressed tar file |
| 348 | - *zip*: ZIP file |
| 349 | |
| 350 | You can register new formats or provide your own archiver for any existing |
| 351 | formats, by using :func:`register_archive_format`. |
| 352 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 353 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 354 | |
| 355 | |
| 356 | .. function:: register_archive_format(name, function, [extra_args, [description]]) |
| 357 | |
Éric Araujo | 14382dc | 2011-07-28 22:49:11 +0200 | [diff] [blame] | 358 | Register an archiver for the format *name*. *function* is a callable that |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 359 | will be used to invoke the archiver. |
| 360 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 361 | 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] | 362 | used as extra keywords arguments when the archiver callable is used. |
| 363 | |
| 364 | *description* is used by :func:`get_archive_formats` which returns the |
| 365 | list of archivers. Defaults to an empty list. |
| 366 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 367 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 368 | |
| 369 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 370 | .. function:: unregister_archive_format(name) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 371 | |
| 372 | Remove the archive format *name* from the list of supported formats. |
| 373 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 374 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 375 | |
| 376 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 377 | .. function:: unpack_archive(filename[, extract_dir[, format]]) |
| 378 | |
| 379 | Unpack an archive. *filename* is the full path of the archive. |
| 380 | |
| 381 | *extract_dir* is the name of the target directory where the archive is |
| 382 | unpacked. If not provided, the current working directory is used. |
| 383 | |
| 384 | *format* is the archive format: one of "zip", "tar", or "gztar". Or any |
| 385 | other format registered with :func:`register_unpack_format`. If not |
| 386 | provided, :func:`unpack_archive` will use the archive file name extension |
| 387 | and see if an unpacker was registered for that extension. In case none is |
| 388 | found, a :exc:`ValueError` is raised. |
| 389 | |
| 390 | .. versionadded:: 3.2 |
| 391 | |
| 392 | |
Raymond Hettinger | 0929b1f | 2011-01-23 11:29:08 +0000 | [diff] [blame] | 393 | .. function:: register_unpack_format(name, extensions, function[, extra_args[, description]]) |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 394 | |
| 395 | Registers an unpack format. *name* is the name of the format and |
| 396 | *extensions* is a list of extensions corresponding to the format, like |
| 397 | ``.zip`` for Zip files. |
| 398 | |
| 399 | *function* is the callable that will be used to unpack archives. The |
| 400 | callable will receive the path of the archive, followed by the directory |
| 401 | the archive must be extracted to. |
| 402 | |
| 403 | When provided, *extra_args* is a sequence of ``(name, value)`` tuples that |
| 404 | will be passed as keywords arguments to the callable. |
| 405 | |
| 406 | *description* can be provided to describe the format, and will be returned |
| 407 | by the :func:`get_unpack_formats` function. |
| 408 | |
| 409 | .. versionadded:: 3.2 |
| 410 | |
| 411 | |
| 412 | .. function:: unregister_unpack_format(name) |
| 413 | |
| 414 | Unregister an unpack format. *name* is the name of the format. |
| 415 | |
| 416 | .. versionadded:: 3.2 |
| 417 | |
| 418 | |
| 419 | .. function:: get_unpack_formats() |
| 420 | |
| 421 | Return a list of all registered formats for unpacking. |
| 422 | Each element of the returned sequence is a tuple |
| 423 | ``(name, extensions, description)``. |
| 424 | |
| 425 | By default :mod:`shutil` provides these formats: |
| 426 | |
| 427 | - *gztar*: gzip'ed tar-file |
Tarek Ziadé | ffa155a | 2010-04-29 13:34:35 +0000 | [diff] [blame] | 428 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.) |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 429 | - *tar*: uncompressed tar file |
| 430 | - *zip*: ZIP file |
| 431 | |
| 432 | You can register new formats or provide your own unpacker for any existing |
| 433 | formats, by using :func:`register_unpack_format`. |
| 434 | |
| 435 | .. versionadded:: 3.2 |
| 436 | |
| 437 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 438 | .. _shutil-archiving-example: |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 439 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 440 | Archiving example |
| 441 | ::::::::::::::::: |
| 442 | |
| 443 | In this example, we create a gzip'ed tar-file archive containing all files |
| 444 | found in the :file:`.ssh` directory of the user:: |
| 445 | |
| 446 | >>> from shutil import make_archive |
| 447 | >>> import os |
| 448 | >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) |
| 449 | >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh')) |
| 450 | >>> make_archive(archive_name, 'gztar', root_dir) |
| 451 | '/Users/tarek/myarchive.tar.gz' |
| 452 | |
| 453 | The resulting archive contains:: |
| 454 | |
| 455 | $ tar -tzvf /Users/tarek/myarchive.tar.gz |
| 456 | drwx------ tarek/staff 0 2010-02-01 16:23:40 ./ |
| 457 | -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys |
| 458 | -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config |
| 459 | -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa |
| 460 | -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub |
| 461 | -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa |
| 462 | -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub |
| 463 | -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] | 464 | |
| 465 | |
| 466 | Querying the size of the output terminal |
| 467 | ---------------------------------------- |
| 468 | |
| 469 | .. versionadded:: 3.3 |
| 470 | |
| 471 | .. function:: get_terminal_size(fallback=(columns, lines)) |
| 472 | |
| 473 | Get the size of the terminal window. |
| 474 | |
| 475 | For each of the two dimensions, the environment variable, ``COLUMNS`` |
| 476 | and ``LINES`` respectively, is checked. If the variable is defined and |
| 477 | the value is a positive integer, it is used. |
| 478 | |
| 479 | When ``COLUMNS`` or ``LINES`` is not defined, which is the common case, |
| 480 | the terminal connected to :data:`sys.__stdout__` is queried |
| 481 | by invoking :func:`os.get_terminal_size`. |
| 482 | |
| 483 | If the terminal size cannot be successfully queried, either because |
| 484 | the system doesn't support querying, or because we are not |
| 485 | connected to a terminal, the value given in ``fallback`` parameter |
| 486 | is used. ``fallback`` defaults to ``(80, 24)`` which is the default |
| 487 | size used by many terminal emulators. |
| 488 | |
| 489 | The value returned is a named tuple of type :class:`os.terminal_size`. |
| 490 | |
| 491 | See also: The Single UNIX Specification, Version 2, |
| 492 | `Other Environment Variables`_. |
| 493 | |
| 494 | .. _`Other Environment Variables`: |
| 495 | http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003 |
| 496 | |