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 | |
| 13 | The :mod:`shutil` module offers a number of high-level operations on files and |
| 14 | collections of files. In particular, functions are provided which support file |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 15 | copying and removal. For operations on individual files, see also the |
| 16 | :mod:`os` module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | da27fd2 | 2007-08-17 00:24:54 +0000 | [diff] [blame] | 18 | .. warning:: |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 19 | |
| 20 | Even the higher-level file copying functions (:func:`copy`, :func:`copy2`) |
| 21 | can't copy all file metadata. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 22 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 23 | 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] | 24 | 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] | 25 | This means that resources will be lost and file type and creator codes will |
| 26 | not be correct. On Windows, file owners, ACLs and alternate data streams |
| 27 | are not copied. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 29 | Directory and files operations |
| 30 | ------------------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | .. function:: copyfileobj(fsrc, fdst[, length]) |
| 33 | |
| 34 | Copy the contents of the file-like object *fsrc* to the file-like object *fdst*. |
| 35 | The integer *length*, if given, is the buffer size. In particular, a negative |
| 36 | *length* value means to copy the data without looping over the source data in |
| 37 | chunks; by default the data is read in chunks to avoid uncontrolled memory |
| 38 | consumption. Note that if the current file position of the *fsrc* object is not |
| 39 | 0, only the contents from the current file position to the end of the file will |
| 40 | be copied. |
| 41 | |
| 42 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 43 | .. function:: copyfile(src, dst) |
| 44 | |
| 45 | Copy the contents (no metadata) of the file named *src* to a file named *dst*. |
| 46 | *dst* must be the complete target file name; look at :func:`copy` for a copy that |
Georg Brandl | af265f4 | 2008-12-07 15:06:20 +0000 | [diff] [blame] | 47 | accepts a target directory path. If *src* and *dst* are the same files, |
| 48 | :exc:`Error` is raised. |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 49 | The destination location must be writable; otherwise, an :exc:`IOError` exception |
| 50 | will be raised. If *dst* already exists, it will be replaced. Special files |
| 51 | such as character or block devices and pipes cannot be copied with this |
| 52 | function. *src* and *dst* are path names given as strings. |
| 53 | |
| 54 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | .. function:: copymode(src, dst) |
| 56 | |
| 57 | Copy the permission bits from *src* to *dst*. The file contents, owner, and |
| 58 | group are unaffected. *src* and *dst* are path names given as strings. |
| 59 | |
| 60 | |
| 61 | .. function:: copystat(src, dst) |
| 62 | |
| 63 | Copy the permission bits, last access time, last modification time, and flags |
| 64 | from *src* to *dst*. The file contents, owner, and group are unaffected. *src* |
| 65 | and *dst* are path names given as strings. |
| 66 | |
| 67 | |
| 68 | .. function:: copy(src, dst) |
| 69 | |
| 70 | Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a |
| 71 | file with the same basename as *src* is created (or overwritten) in the |
| 72 | directory specified. Permission bits are copied. *src* and *dst* are path |
| 73 | names given as strings. |
| 74 | |
| 75 | |
| 76 | .. function:: copy2(src, dst) |
| 77 | |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 78 | Similar to :func:`copy`, but metadata is copied as well -- in fact, this is just |
| 79 | :func:`copy` followed by :func:`copystat`. This is similar to the |
| 80 | Unix command :program:`cp -p`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 83 | .. function:: ignore_patterns(\*patterns) |
| 84 | |
| 85 | This factory function creates a function that can be used as a callable for |
| 86 | :func:`copytree`\'s *ignore* argument, ignoring files and directories that |
| 87 | match one of the glob-style *patterns* provided. See the example below. |
| 88 | |
| 89 | |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 90 | .. 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] | 91 | |
| 92 | Recursively copy an entire directory tree rooted at *src*. The destination |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 93 | directory, named by *dst*, must not already exist; it will be created as well |
| 94 | as missing parent directories. Permissions and times of directories are |
| 95 | copied with :func:`copystat`, individual files are copied using |
| 96 | :func:`copy2`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 98 | If *symlinks* is true, symbolic links in the source tree are represented as |
| 99 | symbolic links in the new tree; if false or omitted, the contents of the |
| 100 | linked files are copied to the new tree. |
| 101 | |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 102 | When *symlinks* is false, if the file pointed by the symlink doesn't |
| 103 | exist, a exception will be added in the list of errors raised in |
| 104 | a :exc:`Error` exception at the end of the copy process. |
| 105 | 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] | 106 | want to silence this exception. Notice that this option has no effect |
| 107 | on platforms that don't support :func:`os.symlink`. |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 108 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 109 | If *ignore* is given, it must be a callable that will receive as its |
| 110 | arguments the directory being visited by :func:`copytree`, and a list of its |
| 111 | contents, as returned by :func:`os.listdir`. Since :func:`copytree` is |
| 112 | called recursively, the *ignore* callable will be called once for each |
| 113 | directory that is copied. The callable must return a sequence of directory |
| 114 | and file names relative to the current directory (i.e. a subset of the items |
| 115 | in its second argument); these names will then be ignored in the copy |
| 116 | process. :func:`ignore_patterns` can be used to create such a callable that |
| 117 | ignores names based on glob-style patterns. |
| 118 | |
| 119 | If exception(s) occur, an :exc:`Error` is raised with a list of reasons. |
| 120 | |
Tarek Ziadé | 5340db3 | 2010-04-19 22:30:51 +0000 | [diff] [blame] | 121 | If *copy_function* is given, it must be a callable that will be used |
| 122 | to copy each file. It will be called with the source path and the |
| 123 | destination path as arguments. By default, :func:`copy2` is used, but any |
| 124 | function that supports the same signature (like :func:`copy`) can be used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
Tarek Ziadé | 5340db3 | 2010-04-19 22:30:51 +0000 | [diff] [blame] | 126 | .. versionchanged:: 3.2 |
| 127 | Added the *copy_function* argument to be able to provide a custom copy |
| 128 | function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 130 | .. versionchanged:: 3.2 |
Tarek Ziadé | fb43751 | 2010-04-20 08:57:33 +0000 | [diff] [blame] | 131 | Added the *ignore_dangling_symlinks* argument to silent dangling symlinks |
| 132 | errors when *symlinks* is false. |
| 133 | |
| 134 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 135 | .. function:: rmtree(path, ignore_errors=False, onerror=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | |
| 137 | .. index:: single: directory; deleting |
| 138 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 139 | Delete an entire directory tree; *path* must point to a directory (but not a |
| 140 | symbolic link to a directory). If *ignore_errors* is true, errors resulting |
| 141 | from failed removals will be ignored; if false or omitted, such errors are |
| 142 | handled by calling a handler specified by *onerror* or, if that is omitted, |
| 143 | they raise an exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 145 | If *onerror* is provided, it must be a callable that accepts three |
| 146 | parameters: *function*, *path*, and *excinfo*. The first parameter, |
| 147 | *function*, is the function which raised the exception; it will be |
| 148 | :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or |
| 149 | :func:`os.rmdir`. The second parameter, *path*, will be the path name passed |
| 150 | to *function*. The third parameter, *excinfo*, will be the exception |
| 151 | information return by :func:`sys.exc_info`. Exceptions raised by *onerror* |
| 152 | will not be caught. |
| 153 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 154 | |
| 155 | .. function:: move(src, dst) |
| 156 | |
| 157 | Recursively move a file or directory to another location. |
| 158 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 159 | If the destination is on the current filesystem, then simply use rename. |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 160 | Otherwise, copy src (with :func:`copy2`) to the dst and then remove src. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | .. exception:: Error |
| 164 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 165 | This exception collects exceptions that raised during a multi-file operation. For |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*, |
| 167 | *dstname*, *exception*). |
| 168 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | |
| 170 | .. _shutil-example: |
| 171 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 172 | copytree example |
| 173 | :::::::::::::::: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
| 175 | This example is the implementation of the :func:`copytree` function, described |
| 176 | above, with the docstring omitted. It demonstrates many of the other functions |
| 177 | provided by this module. :: |
| 178 | |
| 179 | def copytree(src, dst, symlinks=False): |
| 180 | names = os.listdir(src) |
| 181 | os.makedirs(dst) |
| 182 | errors = [] |
| 183 | for name in names: |
| 184 | srcname = os.path.join(src, name) |
| 185 | dstname = os.path.join(dst, name) |
| 186 | try: |
| 187 | if symlinks and os.path.islink(srcname): |
| 188 | linkto = os.readlink(srcname) |
| 189 | os.symlink(linkto, dstname) |
| 190 | elif os.path.isdir(srcname): |
| 191 | copytree(srcname, dstname, symlinks) |
| 192 | else: |
| 193 | copy2(srcname, dstname) |
| 194 | # XXX What about devices, sockets etc.? |
| 195 | except (IOError, os.error) as why: |
| 196 | errors.append((srcname, dstname, str(why))) |
| 197 | # catch the Error from the recursive copytree so that we can |
| 198 | # continue with other files |
| 199 | except Error as err: |
| 200 | errors.extend(err.args[0]) |
| 201 | try: |
| 202 | copystat(src, dst) |
| 203 | except WindowsError: |
| 204 | # can't copy file access times on Windows |
| 205 | pass |
| 206 | except OSError as why: |
| 207 | errors.extend((src, dst, str(why))) |
| 208 | if errors: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 209 | raise Error(errors) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 211 | Another example that uses the :func:`ignore_patterns` helper:: |
| 212 | |
| 213 | from shutil import copytree, ignore_patterns |
| 214 | |
| 215 | copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*')) |
| 216 | |
| 217 | This will copy everything except ``.pyc`` files and files or directories whose |
| 218 | name starts with ``tmp``. |
| 219 | |
| 220 | Another example that uses the *ignore* argument to add a logging call:: |
| 221 | |
| 222 | from shutil import copytree |
| 223 | import logging |
| 224 | |
| 225 | def _logpath(path, names): |
| 226 | logging.info('Working in %s' % path) |
| 227 | return [] # nothing will be ignored |
| 228 | |
| 229 | copytree(source, destination, ignore=_logpath) |
| 230 | |
| 231 | |
| 232 | Archives operations |
| 233 | ------------------- |
| 234 | |
| 235 | .. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]]) |
| 236 | |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 237 | Create an archive file (e.g. zip or tar) and returns its name. |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 238 | |
| 239 | *base_name* is the name of the file to create, including the path, minus |
| 240 | any format-specific extension. *format* is the archive format: one of |
Tarek Ziadé | ffa155a | 2010-04-29 13:34:35 +0000 | [diff] [blame] | 241 | "zip", "tar", "bztar" (if the :mod:`bz2` module is available) or "gztar". |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 242 | |
| 243 | *root_dir* is a directory that will be the root directory of the |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 244 | archive; i.e. we typically chdir into *root_dir* before creating the |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 245 | archive. |
| 246 | |
| 247 | *base_dir* is the directory where we start archiving from; |
Ezio Melotti | cb999a3 | 2010-04-20 11:26:51 +0000 | [diff] [blame] | 248 | 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] | 249 | directories in the archive. |
| 250 | |
| 251 | *root_dir* and *base_dir* both default to the current directory. |
| 252 | |
| 253 | *owner* and *group* are used when creating a tar archive. By default, |
| 254 | uses the current owner and group. |
| 255 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 256 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 257 | |
| 258 | |
| 259 | .. function:: get_archive_formats() |
| 260 | |
| 261 | Returns a list of supported formats for archiving. |
| 262 | Each element of the returned sequence is a tuple ``(name, description)`` |
| 263 | |
| 264 | By default :mod:`shutil` provides these formats: |
| 265 | |
| 266 | - *gztar*: gzip'ed tar-file |
Tarek Ziadé | ffa155a | 2010-04-29 13:34:35 +0000 | [diff] [blame] | 267 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 268 | - *tar*: uncompressed tar file |
| 269 | - *zip*: ZIP file |
| 270 | |
| 271 | You can register new formats or provide your own archiver for any existing |
| 272 | formats, by using :func:`register_archive_format`. |
| 273 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 274 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 275 | |
| 276 | |
| 277 | .. function:: register_archive_format(name, function, [extra_args, [description]]) |
| 278 | |
| 279 | Registers an archiver for the format *name*. *function* is a callable that |
| 280 | will be used to invoke the archiver. |
| 281 | |
| 282 | If given, *extra_args* is a sequence of ``(name, value)`` that will be |
| 283 | used as extra keywords arguments when the archiver callable is used. |
| 284 | |
| 285 | *description* is used by :func:`get_archive_formats` which returns the |
| 286 | list of archivers. Defaults to an empty list. |
| 287 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 288 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 289 | |
| 290 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 291 | .. function:: unregister_archive_format(name) |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 292 | |
| 293 | Remove the archive format *name* from the list of supported formats. |
| 294 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 295 | .. versionadded:: 3.2 |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 296 | |
| 297 | |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 298 | .. function:: unpack_archive(filename[, extract_dir[, format]]) |
| 299 | |
| 300 | Unpack an archive. *filename* is the full path of the archive. |
| 301 | |
| 302 | *extract_dir* is the name of the target directory where the archive is |
| 303 | unpacked. If not provided, the current working directory is used. |
| 304 | |
| 305 | *format* is the archive format: one of "zip", "tar", or "gztar". Or any |
| 306 | other format registered with :func:`register_unpack_format`. If not |
| 307 | provided, :func:`unpack_archive` will use the archive file name extension |
| 308 | and see if an unpacker was registered for that extension. In case none is |
| 309 | found, a :exc:`ValueError` is raised. |
| 310 | |
| 311 | .. versionadded:: 3.2 |
| 312 | |
| 313 | |
| 314 | .. function:: register_unpack_format(name, extensions, function[, extra_args[,description]]) |
| 315 | |
| 316 | Registers an unpack format. *name* is the name of the format and |
| 317 | *extensions* is a list of extensions corresponding to the format, like |
| 318 | ``.zip`` for Zip files. |
| 319 | |
| 320 | *function* is the callable that will be used to unpack archives. The |
| 321 | callable will receive the path of the archive, followed by the directory |
| 322 | the archive must be extracted to. |
| 323 | |
| 324 | When provided, *extra_args* is a sequence of ``(name, value)`` tuples that |
| 325 | will be passed as keywords arguments to the callable. |
| 326 | |
| 327 | *description* can be provided to describe the format, and will be returned |
| 328 | by the :func:`get_unpack_formats` function. |
| 329 | |
| 330 | .. versionadded:: 3.2 |
| 331 | |
| 332 | |
| 333 | .. function:: unregister_unpack_format(name) |
| 334 | |
| 335 | Unregister an unpack format. *name* is the name of the format. |
| 336 | |
| 337 | .. versionadded:: 3.2 |
| 338 | |
| 339 | |
| 340 | .. function:: get_unpack_formats() |
| 341 | |
| 342 | Return a list of all registered formats for unpacking. |
| 343 | Each element of the returned sequence is a tuple |
| 344 | ``(name, extensions, description)``. |
| 345 | |
| 346 | By default :mod:`shutil` provides these formats: |
| 347 | |
| 348 | - *gztar*: gzip'ed tar-file |
Tarek Ziadé | ffa155a | 2010-04-29 13:34:35 +0000 | [diff] [blame] | 349 | - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.) |
Tarek Ziadé | 6ac9172 | 2010-04-28 17:51:36 +0000 | [diff] [blame] | 350 | - *tar*: uncompressed tar file |
| 351 | - *zip*: ZIP file |
| 352 | |
| 353 | You can register new formats or provide your own unpacker for any existing |
| 354 | formats, by using :func:`register_unpack_format`. |
| 355 | |
| 356 | .. versionadded:: 3.2 |
| 357 | |
| 358 | |
| 359 | |
Tarek Ziadé | 396fad7 | 2010-02-23 05:30:31 +0000 | [diff] [blame] | 360 | Archiving example |
| 361 | ::::::::::::::::: |
| 362 | |
| 363 | In this example, we create a gzip'ed tar-file archive containing all files |
| 364 | found in the :file:`.ssh` directory of the user:: |
| 365 | |
| 366 | >>> from shutil import make_archive |
| 367 | >>> import os |
| 368 | >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) |
| 369 | >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh')) |
| 370 | >>> make_archive(archive_name, 'gztar', root_dir) |
| 371 | '/Users/tarek/myarchive.tar.gz' |
| 372 | |
| 373 | The resulting archive contains:: |
| 374 | |
| 375 | $ tar -tzvf /Users/tarek/myarchive.tar.gz |
| 376 | drwx------ tarek/staff 0 2010-02-01 16:23:40 ./ |
| 377 | -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys |
| 378 | -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config |
| 379 | -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa |
| 380 | -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub |
| 381 | -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa |
| 382 | -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub |
| 383 | -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts |
| 384 | |
| 385 | |