Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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> |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 7 | .. partly based on the docstrings |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 8 | |
| 9 | .. index:: |
| 10 | single: file; copying |
| 11 | single: copying files |
| 12 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 13 | **Source code:** :source:`Lib/shutil.py` |
| 14 | |
| 15 | -------------- |
| 16 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 |
Mark Summerfield | ac3d429 | 2007-11-02 08:24:59 +0000 | [diff] [blame] | 19 | copying and removal. For operations on individual files, see also the |
| 20 | :mod:`os` module. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 21 | |
Georg Brandl | bf863b1 | 2007-08-15 19:06:04 +0000 | [diff] [blame] | 22 | .. warning:: |
Georg Brandl | ec32b6b | 2008-01-06 16:12:39 +0000 | [diff] [blame] | 23 | |
Senthil Kumaran | ef18737 | 2012-02-13 23:17:45 +0800 | [diff] [blame] | 24 | Even the higher-level file copying functions (:func:`shutil.copy`, |
| 25 | :func:`shutil.copy2`) can't copy all file metadata. |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 26 | |
Georg Brandl | ec32b6b | 2008-01-06 16:12:39 +0000 | [diff] [blame] | 27 | On POSIX platforms, this means that file owner and group are lost as well |
Georg Brandl | 9af9498 | 2008-09-13 17:41:16 +0000 | [diff] [blame] | 28 | as ACLs. On Mac OS, the resource fork and other metadata are not used. |
Georg Brandl | ec32b6b | 2008-01-06 16:12:39 +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 | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 32 | |
Raymond Hettinger | e0e0822 | 2010-11-06 07:10:31 +0000 | [diff] [blame] | 33 | |
Éric Araujo | c3cc2ac | 2012-02-26 01:10:14 +0100 | [diff] [blame] | 34 | .. _file-operations: |
| 35 | |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 36 | Directory and files operations |
| 37 | ------------------------------ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 38 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Georg Brandl | 786ead6 | 2008-04-19 16:57:43 +0000 | [diff] [blame] | 50 | .. function:: copyfile(src, dst) |
| 51 | |
Senthil Kumaran | ef18737 | 2012-02-13 23:17:45 +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. |
Georg Brandl | 786ead6 | 2008-04-19 16:57:43 +0000 | [diff] [blame] | 56 | The destination location must be writable; otherwise, an :exc:`IOError` exception |
| 57 | will be raised. If *dst* already exists, it will be replaced. Special files |
| 58 | such as character or block devices and pipes cannot be copied with this |
| 59 | function. *src* and *dst* are path names given as strings. |
| 60 | |
| 61 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 62 | .. function:: copymode(src, dst) |
| 63 | |
| 64 | Copy the permission bits from *src* to *dst*. The file contents, owner, and |
| 65 | group are unaffected. *src* and *dst* are path names given as strings. |
| 66 | |
| 67 | |
| 68 | .. function:: copystat(src, dst) |
| 69 | |
| 70 | Copy the permission bits, last access time, last modification time, and flags |
| 71 | from *src* to *dst*. The file contents, owner, and group are unaffected. *src* |
| 72 | and *dst* are path names given as strings. |
| 73 | |
| 74 | |
| 75 | .. function:: copy(src, dst) |
| 76 | |
| 77 | Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a |
| 78 | file with the same basename as *src* is created (or overwritten) in the |
| 79 | directory specified. Permission bits are copied. *src* and *dst* are path |
| 80 | names given as strings. |
| 81 | |
| 82 | |
| 83 | .. function:: copy2(src, dst) |
| 84 | |
Senthil Kumaran | ef18737 | 2012-02-13 23:17:45 +0800 | [diff] [blame] | 85 | Similar to :func:`shutil.copy`, but metadata is copied as well -- in fact, |
| 86 | this is just :func:`shutil.copy` followed by :func:`copystat`. This is |
| 87 | similar to the Unix command :program:`cp -p`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 88 | |
| 89 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 90 | .. function:: ignore_patterns(\*patterns) |
| 91 | |
| 92 | This factory function creates a function that can be used as a callable for |
| 93 | :func:`copytree`\'s *ignore* argument, ignoring files and directories that |
Andrew M. Kuchling | c406084 | 2008-07-06 17:43:16 +0000 | [diff] [blame] | 94 | match one of the glob-style *patterns* provided. See the example below. |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 95 | |
| 96 | .. versionadded:: 2.6 |
| 97 | |
| 98 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 99 | .. function:: copytree(src, dst, symlinks=False, ignore=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 100 | |
| 101 | Recursively copy an entire directory tree rooted at *src*. The destination |
Senthil Kumaran | ef18737 | 2012-02-13 23:17:45 +0800 | [diff] [blame] | 102 | directory, named by *dst*, must not already exist; it will be created as |
| 103 | well as missing parent directories. Permissions and times of directories |
| 104 | are copied with :func:`copystat`, individual files are copied using |
| 105 | :func:`shutil.copy2`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 106 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 107 | If *symlinks* is true, symbolic links in the source tree are represented as |
Senthil Kumaran | 22a7b48 | 2011-08-02 18:50:44 +0800 | [diff] [blame] | 108 | symbolic links in the new tree, but the metadata of the original links is NOT |
| 109 | copied; if false or omitted, the contents and metadata of the linked files |
| 110 | are copied to the new tree. |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 111 | |
| 112 | If *ignore* is given, it must be a callable that will receive as its |
| 113 | arguments the directory being visited by :func:`copytree`, and a list of its |
| 114 | contents, as returned by :func:`os.listdir`. Since :func:`copytree` is |
| 115 | called recursively, the *ignore* callable will be called once for each |
| 116 | directory that is copied. The callable must return a sequence of directory |
| 117 | and file names relative to the current directory (i.e. a subset of the items |
| 118 | in its second argument); these names will then be ignored in the copy |
| 119 | process. :func:`ignore_patterns` can be used to create such a callable that |
| 120 | ignores names based on glob-style patterns. |
| 121 | |
| 122 | If exception(s) occur, an :exc:`Error` is raised with a list of reasons. |
| 123 | |
| 124 | The source code for this should be considered an example rather than the |
| 125 | ultimate tool. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 126 | |
| 127 | .. versionchanged:: 2.3 |
| 128 | :exc:`Error` is raised if any exceptions occur during copying, rather than |
| 129 | printing a message. |
| 130 | |
| 131 | .. versionchanged:: 2.5 |
| 132 | Create intermediate directories needed to create *dst*, rather than raising an |
| 133 | error. Copy permissions and times of directories using :func:`copystat`. |
| 134 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 135 | .. versionchanged:: 2.6 |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 136 | Added the *ignore* argument to be able to influence what is being copied. |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 137 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 138 | |
| 139 | .. function:: rmtree(path[, ignore_errors[, onerror]]) |
| 140 | |
| 141 | .. index:: single: directory; deleting |
| 142 | |
Georg Brandl | 5235398 | 2008-01-20 14:17:42 +0000 | [diff] [blame] | 143 | Delete an entire directory tree; *path* must point to a directory (but not a |
| 144 | symbolic link to a directory). If *ignore_errors* is true, errors resulting |
| 145 | from failed removals will be ignored; if false or omitted, such errors are |
| 146 | handled by calling a handler specified by *onerror* or, if that is omitted, |
| 147 | they raise an exception. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 148 | |
Georg Brandl | 5235398 | 2008-01-20 14:17:42 +0000 | [diff] [blame] | 149 | If *onerror* is provided, it must be a callable that accepts three |
| 150 | parameters: *function*, *path*, and *excinfo*. The first parameter, |
| 151 | *function*, is the function which raised the exception; it will be |
| 152 | :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or |
| 153 | :func:`os.rmdir`. The second parameter, *path*, will be the path name passed |
| 154 | to *function*. The third parameter, *excinfo*, will be the exception |
| 155 | information return by :func:`sys.exc_info`. Exceptions raised by *onerror* |
| 156 | will not be caught. |
| 157 | |
| 158 | .. versionchanged:: 2.6 |
| 159 | Explicitly check for *path* being a symbolic link and raise :exc:`OSError` |
| 160 | in that case. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 161 | |
| 162 | |
| 163 | .. function:: move(src, dst) |
| 164 | |
Éric Araujo | d01aebe | 2011-07-29 12:10:53 +0200 | [diff] [blame] | 165 | Recursively move a file or directory (*src*) to another location (*dst*). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 166 | |
Éric Araujo | d01aebe | 2011-07-29 12:10:53 +0200 | [diff] [blame] | 167 | If the destination is a directory or a symlink to a directory, then *src* is |
| 168 | moved inside that directory. |
| 169 | |
| 170 | The destination directory must not already exist. If the destination already |
| 171 | exists but is not a directory, it may be overwritten depending on |
| 172 | :func:`os.rename` semantics. |
| 173 | |
| 174 | If the destination is on the current filesystem, then :func:`os.rename` is |
Senthil Kumaran | ef18737 | 2012-02-13 23:17:45 +0800 | [diff] [blame] | 175 | used. Otherwise, *src* is copied (using :func:`shutil.copy2`) to *dst* and |
| 176 | then removed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 177 | |
| 178 | .. versionadded:: 2.3 |
| 179 | |
| 180 | |
| 181 | .. exception:: Error |
| 182 | |
Éric Araujo | d01aebe | 2011-07-29 12:10:53 +0200 | [diff] [blame] | 183 | This exception collects exceptions that are raised during a multi-file |
| 184 | operation. For :func:`copytree`, the exception argument is a list of 3-tuples |
| 185 | (*srcname*, *dstname*, *exception*). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 186 | |
| 187 | .. versionadded:: 2.3 |
| 188 | |
Éric Araujo | d01aebe | 2011-07-29 12:10:53 +0200 | [diff] [blame] | 189 | |
Éric Araujo | c3cc2ac | 2012-02-26 01:10:14 +0100 | [diff] [blame] | 190 | .. _copytree-example: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 191 | |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 192 | copytree example |
| 193 | :::::::::::::::: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 194 | |
| 195 | This example is the implementation of the :func:`copytree` function, described |
| 196 | above, with the docstring omitted. It demonstrates many of the other functions |
| 197 | provided by this module. :: |
| 198 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 199 | def copytree(src, dst, symlinks=False, ignore=None): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 200 | names = os.listdir(src) |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 201 | if ignore is not None: |
| 202 | ignored_names = ignore(src, names) |
| 203 | else: |
| 204 | ignored_names = set() |
| 205 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 206 | os.makedirs(dst) |
| 207 | errors = [] |
| 208 | for name in names: |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 209 | if name in ignored_names: |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 210 | continue |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 211 | srcname = os.path.join(src, name) |
| 212 | dstname = os.path.join(dst, name) |
| 213 | try: |
| 214 | if symlinks and os.path.islink(srcname): |
| 215 | linkto = os.readlink(srcname) |
| 216 | os.symlink(linkto, dstname) |
| 217 | elif os.path.isdir(srcname): |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 218 | copytree(srcname, dstname, symlinks, ignore) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 219 | else: |
| 220 | copy2(srcname, dstname) |
| 221 | # XXX What about devices, sockets etc.? |
Andrew Svetlov | 1625d88 | 2012-10-30 21:56:43 +0200 | [diff] [blame] | 222 | except (IOError, os.error) as why: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 223 | errors.append((srcname, dstname, str(why))) |
| 224 | # catch the Error from the recursive copytree so that we can |
| 225 | # continue with other files |
Andrew Svetlov | 1625d88 | 2012-10-30 21:56:43 +0200 | [diff] [blame] | 226 | except Error as err: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 227 | errors.extend(err.args[0]) |
| 228 | try: |
| 229 | copystat(src, dst) |
| 230 | except WindowsError: |
| 231 | # can't copy file access times on Windows |
| 232 | pass |
Andrew Svetlov | 1625d88 | 2012-10-30 21:56:43 +0200 | [diff] [blame] | 233 | except OSError as why: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 234 | errors.extend((src, dst, str(why))) |
| 235 | if errors: |
Georg Brandl | c1edec3 | 2009-06-03 07:25:35 +0000 | [diff] [blame] | 236 | raise Error(errors) |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 237 | |
| 238 | Another example that uses the :func:`ignore_patterns` helper:: |
| 239 | |
| 240 | from shutil import copytree, ignore_patterns |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 241 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 242 | copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*')) |
| 243 | |
| 244 | This will copy everything except ``.pyc`` files and files or directories whose |
| 245 | name starts with ``tmp``. |
| 246 | |
| 247 | Another example that uses the *ignore* argument to add a logging call:: |
| 248 | |
| 249 | from shutil import copytree |
| 250 | import logging |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 251 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 252 | def _logpath(path, names): |
| 253 | logging.info('Working in %s' % path) |
| 254 | return [] # nothing will be ignored |
| 255 | |
| 256 | copytree(source, destination, ignore=_logpath) |
| 257 | |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 258 | |
Éric Araujo | c3cc2ac | 2012-02-26 01:10:14 +0100 | [diff] [blame] | 259 | .. _archiving-operations: |
| 260 | |
| 261 | Archiving operations |
| 262 | -------------------- |
| 263 | |
| 264 | High-level utilities to create and read compressed and archived files are also |
| 265 | provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 266 | |
| 267 | .. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]]) |
| 268 | |
| 269 | Create an archive file (eg. zip or tar) and returns its name. |
| 270 | |
| 271 | *base_name* is the name of the file to create, including the path, minus |
| 272 | any format-specific extension. *format* is the archive format: one of |
Tarek Ziadé | e593fad | 2010-04-20 21:09:06 +0000 | [diff] [blame] | 273 | "zip", "tar", "bztar" or "gztar". |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 274 | |
| 275 | *root_dir* is a directory that will be the root directory of the |
| 276 | archive; ie. we typically chdir into *root_dir* before creating the |
| 277 | archive. |
| 278 | |
| 279 | *base_dir* is the directory where we start archiving from; |
| 280 | ie. *base_dir* will be the common prefix of all files and |
| 281 | directories in the archive. |
| 282 | |
| 283 | *root_dir* and *base_dir* both default to the current directory. |
| 284 | |
| 285 | *owner* and *group* are used when creating a tar archive. By default, |
| 286 | uses the current owner and group. |
| 287 | |
Éric Araujo | c446ce7 | 2012-02-26 01:33:49 +0100 | [diff] [blame] | 288 | *logger* must be an object compatible with :pep:`282`, usually an instance of |
| 289 | :class:`logging.Logger`. |
Éric Araujo | 99c4c34 | 2011-08-19 02:51:17 +0200 | [diff] [blame] | 290 | |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 291 | .. versionadded:: 2.7 |
| 292 | |
| 293 | |
| 294 | .. function:: get_archive_formats() |
| 295 | |
Éric Araujo | d01aebe | 2011-07-29 12:10:53 +0200 | [diff] [blame] | 296 | Return a list of supported formats for archiving. |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 297 | Each element of the returned sequence is a tuple ``(name, description)`` |
| 298 | |
| 299 | By default :mod:`shutil` provides these formats: |
| 300 | |
| 301 | - *gztar*: gzip'ed tar-file |
| 302 | - *bztar*: bzip2'ed tar-file |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 303 | - *tar*: uncompressed tar file |
| 304 | - *zip*: ZIP file |
| 305 | |
| 306 | You can register new formats or provide your own archiver for any existing |
| 307 | formats, by using :func:`register_archive_format`. |
| 308 | |
| 309 | .. versionadded:: 2.7 |
| 310 | |
| 311 | |
| 312 | .. function:: register_archive_format(name, function, [extra_args, [description]]) |
| 313 | |
Éric Araujo | d01aebe | 2011-07-29 12:10:53 +0200 | [diff] [blame] | 314 | Register an archiver for the format *name*. *function* is a callable that |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 315 | will be used to invoke the archiver. |
| 316 | |
| 317 | If given, *extra_args* is a sequence of ``(name, value)`` that will be |
| 318 | used as extra keywords arguments when the archiver callable is used. |
| 319 | |
| 320 | *description* is used by :func:`get_archive_formats` which returns the |
| 321 | list of archivers. Defaults to an empty list. |
| 322 | |
| 323 | .. versionadded:: 2.7 |
| 324 | |
| 325 | |
| 326 | .. function:: unregister_archive_format(name) |
| 327 | |
| 328 | Remove the archive format *name* from the list of supported formats. |
| 329 | |
| 330 | .. versionadded:: 2.7 |
| 331 | |
| 332 | |
Éric Araujo | c3cc2ac | 2012-02-26 01:10:14 +0100 | [diff] [blame] | 333 | .. _archiving-example: |
| 334 | |
Tarek Ziadé | 48cc8dc | 2010-02-23 05:16:41 +0000 | [diff] [blame] | 335 | Archiving example |
| 336 | ::::::::::::::::: |
| 337 | |
| 338 | In this example, we create a gzip'ed tar-file archive containing all files |
| 339 | found in the :file:`.ssh` directory of the user:: |
| 340 | |
| 341 | >>> from shutil import make_archive |
| 342 | >>> import os |
| 343 | >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) |
| 344 | >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh')) |
| 345 | >>> make_archive(archive_name, 'gztar', root_dir) |
| 346 | '/Users/tarek/myarchive.tar.gz' |
| 347 | |
| 348 | The resulting archive contains:: |
| 349 | |
| 350 | $ tar -tzvf /Users/tarek/myarchive.tar.gz |
| 351 | drwx------ tarek/staff 0 2010-02-01 16:23:40 ./ |
| 352 | -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys |
| 353 | -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config |
| 354 | -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa |
| 355 | -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub |
| 356 | -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa |
| 357 | -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub |
| 358 | -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts |