1:mod:`shutil` --- High-level file operations
2============================================
3
4.. module:: shutil
5   :synopsis: High-level file operations, including copying.
6
7.. sectionauthor:: Fred L. Drake, Jr. <[email protected]>
8.. partly based on the docstrings
9
10**Source code:** :source:`Lib/shutil.py`
11
12.. index::
13   single: file; copying
14   single: copying files
15
16--------------
17
18The :mod:`shutil` module offers a number of high-level operations on files and
19collections of files.  In particular, functions are provided  which support file
20copying and removal. For operations on individual files, see also the
21:mod:`os` module.
22
23.. warning::
24
25   Even the higher-level file copying functions (:func:`shutil.copy`,
26   :func:`shutil.copy2`) cannot copy all file metadata.
27
28   On POSIX platforms, this means that file owner and group are lost as well
29   as ACLs.  On Mac OS, the resource fork and other metadata are not used.
30   This means that resources will be lost and file type and creator codes will
31   not be correct. On Windows, file owners, ACLs and alternate data streams
32   are not copied.
33
34
35.. _file-operations:
36
37Directory and files operations
38------------------------------
39
40.. function:: copyfileobj(fsrc, fdst[, length])
41
42   Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
43   The integer *length*, if given, is the buffer size. In particular, a negative
44   *length* value means to copy the data without looping over the source data in
45   chunks; by default the data is read in chunks to avoid uncontrolled memory
46   consumption. Note that if the current file position of the *fsrc* object is not
47   0, only the contents from the current file position to the end of the file will
48   be copied.
49
50
51.. function:: copyfile(src, dst, *, follow_symlinks=True)
52
53   Copy the contents (no metadata) of the file named *src* to a file named
54   *dst* and return *dst* in the most efficient way possible.
55   *src* and *dst* are path-like objects or path names given as strings.
56
57   *dst* must be the complete target file name; look at :func:`~shutil.copy`
58   for a copy that accepts a target directory path.  If *src* and *dst*
59   specify the same file, :exc:`SameFileError` is raised.
60
61   The destination location must be writable; otherwise, an :exc:`OSError`
62   exception will be raised. If *dst* already exists, it will be replaced.
63   Special files such as character or block devices and pipes cannot be
64   copied with this function.
65
66   If *follow_symlinks* is false and *src* is a symbolic link,
67   a new symbolic link will be created instead of copying the
68   file *src* points to.
69
70   .. audit-event:: shutil.copyfile src,dst shutil.copyfile
71
72   .. versionchanged:: 3.3
73      :exc:`IOError` used to be raised instead of :exc:`OSError`.
74      Added *follow_symlinks* argument.
75      Now returns *dst*.
76
77   .. versionchanged:: 3.4
78      Raise :exc:`SameFileError` instead of :exc:`Error`.  Since the former is
79      a subclass of the latter, this change is backward compatible.
80
81   .. versionchanged:: 3.8
82      Platform-specific fast-copy syscalls may be used internally in order to
83      copy the file more efficiently. See
84      :ref:`shutil-platform-dependent-efficient-copy-operations` section.
85
86.. exception:: SameFileError
87
88   This exception is raised if source and destination in :func:`copyfile`
89   are the same file.
90
91   .. versionadded:: 3.4
92
93
94.. function:: copymode(src, dst, *, follow_symlinks=True)
95
96   Copy the permission bits from *src* to *dst*.  The file contents, owner, and
97   group are unaffected.  *src* and *dst* are path-like objects or path names
98   given as strings.
99   If *follow_symlinks* is false, and both *src* and *dst* are symbolic links,
100   :func:`copymode` will attempt to modify the mode of *dst* itself (rather
101   than the file it points to).  This functionality is not available on every
102   platform; please see :func:`copystat` for more information.  If
103   :func:`copymode` cannot modify symbolic links on the local platform, and it
104   is asked to do so, it will do nothing and return.
105
106   .. audit-event:: shutil.copymode src,dst shutil.copymode
107
108   .. versionchanged:: 3.3
109      Added *follow_symlinks* argument.
110
111.. function:: copystat(src, dst, *, follow_symlinks=True)
112
113   Copy the permission bits, last access time, last modification time, and
114   flags from *src* to *dst*.  On Linux, :func:`copystat` also copies the
115   "extended attributes" where possible.  The file contents, owner, and
116   group are unaffected.  *src* and *dst* are path-like objects or path
117   names given as strings.
118
119   If *follow_symlinks* is false, and *src* and *dst* both
120   refer to symbolic links, :func:`copystat` will operate on
121   the symbolic links themselves rather than the files the
122   symbolic links refer to—reading the information from the
123   *src* symbolic link, and writing the information to the
124   *dst* symbolic link.
125
126   .. note::
127
128      Not all platforms provide the ability to examine and
129      modify symbolic links.  Python itself can tell you what
130      functionality is locally available.
131
132      * If ``os.chmod in os.supports_follow_symlinks`` is
133        ``True``, :func:`copystat` can modify the permission
134        bits of a symbolic link.
135
136      * If ``os.utime in os.supports_follow_symlinks`` is
137        ``True``, :func:`copystat` can modify the last access
138        and modification times of a symbolic link.
139
140      * If ``os.chflags in os.supports_follow_symlinks`` is
141        ``True``, :func:`copystat` can modify the flags of
142        a symbolic link.  (``os.chflags`` is not available on
143        all platforms.)
144
145      On platforms where some or all of this functionality
146      is unavailable, when asked to modify a symbolic link,
147      :func:`copystat` will copy everything it can.
148      :func:`copystat` never returns failure.
149
150      Please see :data:`os.supports_follow_symlinks`
151      for more information.
152
153   .. audit-event:: shutil.copystat src,dst shutil.copystat
154
155   .. versionchanged:: 3.3
156      Added *follow_symlinks* argument and support for Linux extended attributes.
157
158.. function:: copy(src, dst, *, follow_symlinks=True)
159
160   Copies the file *src* to the file or directory *dst*.  *src* and *dst*
161   should be :term:`path-like objects <path-like object>` or strings.  If
162   *dst* specifies a directory, the file will be copied into *dst* using the
163   base filename from *src*. If *dst* specifies a file that already exists,
164   it will be replaced. Returns the path to the newly created file.
165
166   If *follow_symlinks* is false, and *src* is a symbolic link,
167   *dst* will be created as a symbolic link.  If *follow_symlinks*
168   is true and *src* is a symbolic link, *dst* will be a copy of
169   the file *src* refers to.
170
171   :func:`~shutil.copy` copies the file data and the file's permission
172   mode (see :func:`os.chmod`).  Other metadata, like the
173   file's creation and modification times, is not preserved.
174   To preserve all file metadata from the original, use
175   :func:`~shutil.copy2` instead.
176
177   .. audit-event:: shutil.copyfile src,dst shutil.copy
178
179   .. audit-event:: shutil.copymode src,dst shutil.copy
180
181   .. versionchanged:: 3.3
182      Added *follow_symlinks* argument.
183      Now returns path to the newly created file.
184
185   .. versionchanged:: 3.8
186      Platform-specific fast-copy syscalls may be used internally in order to
187      copy the file more efficiently. See
188      :ref:`shutil-platform-dependent-efficient-copy-operations` section.
189
190.. function:: copy2(src, dst, *, follow_symlinks=True)
191
192   Identical to :func:`~shutil.copy` except that :func:`copy2`
193   also attempts to preserve file metadata.
194
195   When *follow_symlinks* is false, and *src* is a symbolic
196   link, :func:`copy2` attempts to copy all metadata from the
197   *src* symbolic link to the newly created *dst* symbolic link.
198   However, this functionality is not available on all platforms.
199   On platforms where some or all of this functionality is
200   unavailable, :func:`copy2` will preserve all the metadata
201   it can; :func:`copy2` never raises an exception because it
202   cannot preserve file metadata.
203
204   :func:`copy2` uses :func:`copystat` to copy the file metadata.
205   Please see :func:`copystat` for more information
206   about platform support for modifying symbolic link metadata.
207
208   .. audit-event:: shutil.copyfile src,dst shutil.copy2
209
210   .. audit-event:: shutil.copystat src,dst shutil.copy2
211
212   .. versionchanged:: 3.3
213      Added *follow_symlinks* argument, try to copy extended
214      file system attributes too (currently Linux only).
215      Now returns path to the newly created file.
216
217   .. versionchanged:: 3.8
218      Platform-specific fast-copy syscalls may be used internally in order to
219      copy the file more efficiently. See
220      :ref:`shutil-platform-dependent-efficient-copy-operations` section.
221
222.. function:: ignore_patterns(*patterns)
223
224   This factory function creates a function that can be used as a callable for
225   :func:`copytree`\'s *ignore* argument, ignoring files and directories that
226   match one of the glob-style *patterns* provided.  See the example below.
227
228
229.. function:: copytree(src, dst, symlinks=False, ignore=None, \
230              copy_function=copy2, ignore_dangling_symlinks=False, \
231              dirs_exist_ok=False)
232
233   Recursively copy an entire directory tree rooted at *src* to a directory
234   named *dst* and return the destination directory.  All intermediate
235   directories needed to contain *dst* will also be created by default.
236
237   Permissions and times of directories are copied with :func:`copystat`,
238   individual files are copied using :func:`~shutil.copy2`.
239
240   If *symlinks* is true, symbolic links in the source tree are represented as
241   symbolic links in the new tree and the metadata of the original links will
242   be copied as far as the platform allows; if false or omitted, the contents
243   and metadata of the linked files are copied to the new tree.
244
245   When *symlinks* is false, if the file pointed by the symlink doesn't
246   exist, an exception will be added in the list of errors raised in
247   an :exc:`Error` exception at the end of the copy process.
248   You can set the optional *ignore_dangling_symlinks* flag to true if you
249   want to silence this exception. Notice that this option has no effect
250   on platforms that don't support :func:`os.symlink`.
251
252   If *ignore* is given, it must be a callable that will receive as its
253   arguments the directory being visited by :func:`copytree`, and a list of its
254   contents, as returned by :func:`os.listdir`.  Since :func:`copytree` is
255   called recursively, the *ignore* callable will be called once for each
256   directory that is copied.  The callable must return a sequence of directory
257   and file names relative to the current directory (i.e. a subset of the items
258   in its second argument); these names will then be ignored in the copy
259   process.  :func:`ignore_patterns` can be used to create such a callable that
260   ignores names based on glob-style patterns.
261
262   If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
263
264   If *copy_function* is given, it must be a callable that will be used to copy
265   each file. It will be called with the source path and the destination path
266   as arguments. By default, :func:`~shutil.copy2` is used, but any function
267   that supports the same signature (like :func:`~shutil.copy`) can be used.
268
269   If *dirs_exist_ok* is false (the default) and *dst* already exists, a
270   :exc:`FileExistsError` is raised. If *dirs_exist_ok* is true, the copying
271   operation will continue if it encounters existing directories, and files
272   within the *dst* tree will be overwritten by corresponding files from the
273   *src* tree.
274
275   .. audit-event:: shutil.copytree src,dst shutil.copytree
276
277   .. versionchanged:: 3.3
278      Copy metadata when *symlinks* is false.
279      Now returns *dst*.
280
281   .. versionchanged:: 3.2
282      Added the *copy_function* argument to be able to provide a custom copy
283      function.
284      Added the *ignore_dangling_symlinks* argument to silence dangling symlinks
285      errors when *symlinks* is false.
286
287   .. versionchanged:: 3.8
288      Platform-specific fast-copy syscalls may be used internally in order to
289      copy the file more efficiently. See
290      :ref:`shutil-platform-dependent-efficient-copy-operations` section.
291
292   .. versionadded:: 3.8
293      The *dirs_exist_ok* parameter.
294
295.. function:: rmtree(path, ignore_errors=False, onerror=None, *, dir_fd=None)
296
297   .. index:: single: directory; deleting
298
299   Delete an entire directory tree; *path* must point to a directory (but not a
300   symbolic link to a directory).  If *ignore_errors* is true, errors resulting
301   from failed removals will be ignored; if false or omitted, such errors are
302   handled by calling a handler specified by *onerror* or, if that is omitted,
303   they raise an exception.
304
305   This function can support :ref:`paths relative to directory descriptors
306   <dir_fd>`.
307
308   .. note::
309
310      On platforms that support the necessary fd-based functions a symlink
311      attack resistant version of :func:`rmtree` is used by default.  On other
312      platforms, the :func:`rmtree` implementation is susceptible to a symlink
313      attack: given proper timing and circumstances, attackers can manipulate
314      symlinks on the filesystem to delete files they wouldn't be able to access
315      otherwise.  Applications can use the :data:`rmtree.avoids_symlink_attacks`
316      function attribute to determine which case applies.
317
318   If *onerror* is provided, it must be a callable that accepts three
319   parameters: *function*, *path*, and *excinfo*.
320
321   The first parameter, *function*, is the function which raised the exception;
322   it depends on the platform and implementation.  The second parameter,
323   *path*, will be the path name passed to *function*.  The third parameter,
324   *excinfo*, will be the exception information returned by
325   :func:`sys.exc_info`.  Exceptions raised by *onerror* will not be caught.
326
327   .. audit-event:: shutil.rmtree path,dir_fd shutil.rmtree
328
329   .. versionchanged:: 3.3
330      Added a symlink attack resistant version that is used automatically
331      if platform supports fd-based functions.
332
333   .. versionchanged:: 3.8
334      On Windows, will no longer delete the contents of a directory junction
335      before removing the junction.
336
337   .. versionchanged:: 3.11
338      The *dir_fd* parameter.
339
340   .. attribute:: rmtree.avoids_symlink_attacks
341
342      Indicates whether the current platform and implementation provides a
343      symlink attack resistant version of :func:`rmtree`.  Currently this is
344      only true for platforms supporting fd-based directory access functions.
345
346      .. versionadded:: 3.3
347
348
349.. function:: move(src, dst, copy_function=copy2)
350
351   Recursively move a file or directory (*src*) to another location (*dst*)
352   and return the destination.
353
354   If the destination is an existing directory, then *src* is moved inside that
355   directory. If the destination already exists but is not a directory, it may
356   be overwritten depending on :func:`os.rename` semantics.
357
358   If the destination is on the current filesystem, then :func:`os.rename` is
359   used. Otherwise, *src* is copied to *dst* using *copy_function* and then
360   removed.  In case of symlinks, a new symlink pointing to the target of *src*
361   will be created in or as *dst* and *src* will be removed.
362
363   If *copy_function* is given, it must be a callable that takes two arguments
364   *src* and *dst*, and will be used to copy *src* to *dst* if
365   :func:`os.rename` cannot be used.  If the source is a directory,
366   :func:`copytree` is called, passing it the :func:`copy_function`. The
367   default *copy_function* is :func:`copy2`.  Using :func:`~shutil.copy` as the
368   *copy_function* allows the move to succeed when it is not possible to also
369   copy the metadata, at the expense of not copying any of the metadata.
370
371   .. audit-event:: shutil.move src,dst shutil.move
372
373   .. versionchanged:: 3.3
374      Added explicit symlink handling for foreign filesystems, thus adapting
375      it to the behavior of GNU's :program:`mv`.
376      Now returns *dst*.
377
378   .. versionchanged:: 3.5
379      Added the *copy_function* keyword argument.
380
381   .. versionchanged:: 3.8
382      Platform-specific fast-copy syscalls may be used internally in order to
383      copy the file more efficiently. See
384      :ref:`shutil-platform-dependent-efficient-copy-operations` section.
385
386   .. versionchanged:: 3.9
387      Accepts a :term:`path-like object` for both *src* and *dst*.
388
389.. function:: disk_usage(path)
390
391   Return disk usage statistics about the given path as a :term:`named tuple`
392   with the attributes *total*, *used* and *free*, which are the amount of
393   total, used and free space, in bytes. *path* may be a file or a
394   directory.
395
396   .. versionadded:: 3.3
397
398   .. versionchanged:: 3.8
399     On Windows, *path* can now be a file or directory.
400
401   .. availability:: Unix, Windows.
402
403.. function:: chown(path, user=None, group=None)
404
405   Change owner *user* and/or *group* of the given *path*.
406
407   *user* can be a system user name or a uid; the same applies to *group*. At
408   least one argument is required.
409
410   See also :func:`os.chown`, the underlying function.
411
412   .. audit-event:: shutil.chown path,user,group shutil.chown
413
414   .. availability:: Unix.
415
416   .. versionadded:: 3.3
417
418
419.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)
420
421   Return the path to an executable which would be run if the given *cmd* was
422   called.  If no *cmd* would be called, return ``None``.
423
424   *mode* is a permission mask passed to :func:`os.access`, by default
425   determining if the file exists and executable.
426
427   When no *path* is specified, the results of :func:`os.environ` are used,
428   returning either the "PATH" value or a fallback of :attr:`os.defpath`.
429
430   On Windows, the current directory is always prepended to the *path* whether
431   or not you use the default or provide your own, which is the behavior the
432   command shell uses when finding executables.  Additionally, when finding the
433   *cmd* in the *path*, the ``PATHEXT`` environment variable is checked.  For
434   example, if you call ``shutil.which("python")``, :func:`which` will search
435   ``PATHEXT`` to know that it should look for ``python.exe`` within the *path*
436   directories.  For example, on Windows::
437
438      >>> shutil.which("python")
439      'C:\\Python33\\python.EXE'
440
441   .. versionadded:: 3.3
442
443   .. versionchanged:: 3.8
444      The :class:`bytes` type is now accepted.  If *cmd* type is
445      :class:`bytes`, the result type is also :class:`bytes`.
446
447.. exception:: Error
448
449   This exception collects exceptions that are raised during a multi-file
450   operation. For :func:`copytree`, the exception argument is a list of 3-tuples
451   (*srcname*, *dstname*, *exception*).
452
453.. _shutil-platform-dependent-efficient-copy-operations:
454
455Platform-dependent efficient copy operations
456~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
457
458Starting from Python 3.8, all functions involving a file copy
459(:func:`copyfile`, :func:`~shutil.copy`, :func:`copy2`,
460:func:`copytree`, and :func:`move`) may use
461platform-specific "fast-copy" syscalls in order to copy the file more
462efficiently (see :issue:`33671`).
463"fast-copy" means that the copying operation occurs within the kernel, avoiding
464the use of userspace buffers in Python as in "``outfd.write(infd.read())``".
465
466On macOS `fcopyfile`_ is used to copy the file content (not metadata).
467
468On Linux :func:`os.sendfile` is used.
469
470On Windows :func:`shutil.copyfile` uses a bigger default buffer size (1 MiB
471instead of 64 KiB) and a :func:`memoryview`-based variant of
472:func:`shutil.copyfileobj` is used.
473
474If the fast-copy operation fails and no data was written in the destination
475file then shutil will silently fallback on using less efficient
476:func:`copyfileobj` function internally.
477
478.. versionchanged:: 3.8
479
480.. _shutil-copytree-example:
481
482copytree example
483~~~~~~~~~~~~~~~~
484
485An example that uses the :func:`ignore_patterns` helper::
486
487   from shutil import copytree, ignore_patterns
488
489   copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
490
491This will copy everything except ``.pyc`` files and files or directories whose
492name starts with ``tmp``.
493
494Another example that uses the *ignore* argument to add a logging call::
495
496   from shutil import copytree
497   import logging
498
499   def _logpath(path, names):
500       logging.info('Working in %s', path)
501       return []   # nothing will be ignored
502
503   copytree(source, destination, ignore=_logpath)
504
505
506.. _shutil-rmtree-example:
507
508rmtree example
509~~~~~~~~~~~~~~
510
511This example shows how to remove a directory tree on Windows where some
512of the files have their read-only bit set. It uses the onerror callback
513to clear the readonly bit and reattempt the remove. Any subsequent failure
514will propagate. ::
515
516    import os, stat
517    import shutil
518
519    def remove_readonly(func, path, _):
520        "Clear the readonly bit and reattempt the removal"
521        os.chmod(path, stat.S_IWRITE)
522        func(path)
523
524    shutil.rmtree(directory, onerror=remove_readonly)
525
526.. _archiving-operations:
527
528Archiving operations
529--------------------
530
531.. versionadded:: 3.2
532
533.. versionchanged:: 3.5
534    Added support for the *xztar* format.
535
536
537High-level utilities to create and read compressed and archived files are also
538provided.  They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
539
540.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
541
542   Create an archive file (such as zip or tar) and return its name.
543
544   *base_name* is the name of the file to create, including the path, minus
545   any format-specific extension. *format* is the archive format: one of
546   "zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the
547   :mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is
548   available), or "xztar" (if the :mod:`lzma` module is available).
549
550   *root_dir* is a directory that will be the root directory of the
551   archive, all paths in the archive will be relative to it; for example,
552   we typically chdir into *root_dir* before creating the archive.
553
554   *base_dir* is the directory where we start archiving from;
555   i.e. *base_dir* will be the common prefix of all files and
556   directories in the archive.  *base_dir* must be given relative
557   to *root_dir*.  See :ref:`shutil-archiving-example-with-basedir` for how to
558   use *base_dir* and *root_dir* together.
559
560   *root_dir* and *base_dir* both default to the current directory.
561
562   If *dry_run* is true, no archive is created, but the operations that would be
563   executed are logged to *logger*.
564
565   *owner* and *group* are used when creating a tar archive. By default,
566   uses the current owner and group.
567
568   *logger* must be an object compatible with :pep:`282`, usually an instance of
569   :class:`logging.Logger`.
570
571   The *verbose* argument is unused and deprecated.
572
573   .. audit-event:: shutil.make_archive base_name,format,root_dir,base_dir shutil.make_archive
574
575   .. note::
576
577      This function is not thread-safe when custom archivers registered
578      with :func:`register_archive_format` are used.  In this case it
579      temporarily changes the current working directory of the process
580      to perform archiving.
581
582   .. versionchanged:: 3.8
583      The modern pax (POSIX.1-2001) format is now used instead of
584      the legacy GNU format for archives created with ``format="tar"``.
585
586   .. versionchanged:: 3.10.6
587      This function is now made thread-safe during creation of standard
588      ``.zip`` and tar archives.
589
590.. function:: get_archive_formats()
591
592   Return a list of supported formats for archiving.
593   Each element of the returned sequence is a tuple ``(name, description)``.
594
595   By default :mod:`shutil` provides these formats:
596
597   - *zip*: ZIP file (if the :mod:`zlib` module is available).
598   - *tar*: Uncompressed tar file. Uses POSIX.1-2001 pax format for new archives.
599   - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
600   - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
601   - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
602
603   You can register new formats or provide your own archiver for any existing
604   formats, by using :func:`register_archive_format`.
605
606
607.. function:: register_archive_format(name, function, [extra_args, [description]])
608
609   Register an archiver for the format *name*.
610
611   *function* is the callable that will be used to unpack archives. The callable
612   will receive the *base_name* of the file to create, followed by the
613   *base_dir* (which defaults to :data:`os.curdir`) to start archiving from.
614   Further arguments are passed as keyword arguments: *owner*, *group*,
615   *dry_run* and *logger* (as passed in :func:`make_archive`).
616
617   If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be
618   used as extra keywords arguments when the archiver callable is used.
619
620   *description* is used by :func:`get_archive_formats` which returns the
621   list of archivers.  Defaults to an empty string.
622
623
624.. function:: unregister_archive_format(name)
625
626   Remove the archive format *name* from the list of supported formats.
627
628
629.. function:: unpack_archive(filename[, extract_dir[, format[, filter]]])
630
631   Unpack an archive. *filename* is the full path of the archive.
632
633   *extract_dir* is the name of the target directory where the archive is
634   unpacked. If not provided, the current working directory is used.
635
636   *format* is the archive format: one of "zip", "tar", "gztar", "bztar", or
637   "xztar".  Or any other format registered with
638   :func:`register_unpack_format`.  If not provided, :func:`unpack_archive`
639   will use the archive file name extension and see if an unpacker was
640   registered for that extension.  In case none is found,
641   a :exc:`ValueError` is raised.
642
643   The keyword-only *filter* argument, which was added in Python 3.11.4,
644   is passed to the underlying unpacking function.
645   For zip files, *filter* is not accepted.
646   For tar files, it is recommended to set it to ``'data'``,
647   unless using features specific to tar and UNIX-like filesystems.
648   (See :ref:`tarfile-extraction-filter` for details.)
649   The ``'data'`` filter will become the default for tar files
650   in Python 3.14.
651
652   .. audit-event:: shutil.unpack_archive filename,extract_dir,format shutil.unpack_archive
653
654   .. warning::
655
656      Never extract archives from untrusted sources without prior inspection.
657      It is possible that files are created outside of the path specified in
658      the *extract_dir* argument, e.g. members that have absolute filenames
659      starting with "/" or filenames with two dots "..".
660
661   .. versionchanged:: 3.7
662      Accepts a :term:`path-like object` for *filename* and *extract_dir*.
663
664   .. versionchanged:: 3.11.4
665      Added the *filter* argument.
666
667.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
668
669   Registers an unpack format. *name* is the name of the format and
670   *extensions* is a list of extensions corresponding to the format, like
671   ``.zip`` for Zip files.
672
673   *function* is the callable that will be used to unpack archives. The
674   callable will receive:
675
676   - the path of the archive, as a positional argument;
677   - the directory the archive must be extracted to, as a positional argument;
678   - possibly a *filter* keyword argument, if it was given to
679     :func:`unpack_archive`;
680   - additional keyword arguments, specified by *extra_args* as a sequence
681     of ``(name, value)`` tuples.
682
683   *description* can be provided to describe the format, and will be returned
684   by the :func:`get_unpack_formats` function.
685
686
687.. function:: unregister_unpack_format(name)
688
689   Unregister an unpack format. *name* is the name of the format.
690
691
692.. function:: get_unpack_formats()
693
694   Return a list of all registered formats for unpacking.
695   Each element of the returned sequence is a tuple
696   ``(name, extensions, description)``.
697
698   By default :mod:`shutil` provides these formats:
699
700   - *zip*: ZIP file (unpacking compressed files works only if the corresponding
701     module is available).
702   - *tar*: uncompressed tar file.
703   - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
704   - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
705   - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
706
707   You can register new formats or provide your own unpacker for any existing
708   formats, by using :func:`register_unpack_format`.
709
710
711.. _shutil-archiving-example:
712
713Archiving example
714~~~~~~~~~~~~~~~~~
715
716In this example, we create a gzip'ed tar-file archive containing all files
717found in the :file:`.ssh` directory of the user::
718
719    >>> from shutil import make_archive
720    >>> import os
721    >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
722    >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
723    >>> make_archive(archive_name, 'gztar', root_dir)
724    '/Users/tarek/myarchive.tar.gz'
725
726The resulting archive contains:
727
728.. code-block:: shell-session
729
730    $ tar -tzvf /Users/tarek/myarchive.tar.gz
731    drwx------ tarek/staff       0 2010-02-01 16:23:40 ./
732    -rw-r--r-- tarek/staff     609 2008-06-09 13:26:54 ./authorized_keys
733    -rwxr-xr-x tarek/staff      65 2008-06-09 13:26:54 ./config
734    -rwx------ tarek/staff     668 2008-06-09 13:26:54 ./id_dsa
735    -rwxr-xr-x tarek/staff     609 2008-06-09 13:26:54 ./id_dsa.pub
736    -rw------- tarek/staff    1675 2008-06-09 13:26:54 ./id_rsa
737    -rw-r--r-- tarek/staff     397 2008-06-09 13:26:54 ./id_rsa.pub
738    -rw-r--r-- tarek/staff   37192 2010-02-06 18:23:10 ./known_hosts
739
740
741.. _shutil-archiving-example-with-basedir:
742
743Archiving example with *base_dir*
744~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
745
746In this example, similar to the `one above <shutil-archiving-example_>`_,
747we show how to use :func:`make_archive`, but this time with the usage of
748*base_dir*.  We now have the following directory structure:
749
750.. code-block:: shell-session
751
752    $ tree tmp
753    tmp
754    └── root
755        └── structure
756            ├── content
757                └── please_add.txt
758            └── do_not_add.txt
759
760In the final archive, :file:`please_add.txt` should be included, but
761:file:`do_not_add.txt` should not.  Therefore we use the following::
762
763    >>> from shutil import make_archive
764    >>> import os
765    >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
766    >>> make_archive(
767    ...     archive_name,
768    ...     'tar',
769    ...     root_dir='tmp/root',
770    ...     base_dir='structure/content',
771    ... )
772    '/Users/tarek/my_archive.tar'
773
774Listing the files in the resulting archive gives us:
775
776.. code-block:: shell-session
777
778    $ python -m tarfile -l /Users/tarek/myarchive.tar
779    structure/content/
780    structure/content/please_add.txt
781
782
783Querying the size of the output terminal
784----------------------------------------
785
786.. function:: get_terminal_size(fallback=(columns, lines))
787
788   Get the size of the terminal window.
789
790   For each of the two dimensions, the environment variable, ``COLUMNS``
791   and ``LINES`` respectively, is checked. If the variable is defined and
792   the value is a positive integer, it is used.
793
794   When ``COLUMNS`` or ``LINES`` is not defined, which is the common case,
795   the terminal connected to :data:`sys.__stdout__` is queried
796   by invoking :func:`os.get_terminal_size`.
797
798   If the terminal size cannot be successfully queried, either because
799   the system doesn't support querying, or because we are not
800   connected to a terminal, the value given in ``fallback`` parameter
801   is used. ``fallback`` defaults to ``(80, 24)`` which is the default
802   size used by many terminal emulators.
803
804   The value returned is a named tuple of type :class:`os.terminal_size`.
805
806   See also: The Single UNIX Specification, Version 2,
807   `Other Environment Variables`_.
808
809   .. versionadded:: 3.3
810
811   .. versionchanged:: 3.11
812      The ``fallback`` values are also used if :func:`os.get_terminal_size`
813      returns zeroes.
814
815.. _`fcopyfile`:
816   http://www.manpagez.com/man/3/copyfile/
817
818.. _`Other Environment Variables`:
819   https://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003
820