17db96d56Sopenharmony_ci:mod:`shutil` --- High-level file operations 27db96d56Sopenharmony_ci============================================ 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: shutil 57db96d56Sopenharmony_ci :synopsis: High-level file operations, including copying. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 87db96d56Sopenharmony_ci.. partly based on the docstrings 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci**Source code:** :source:`Lib/shutil.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci.. index:: 137db96d56Sopenharmony_ci single: file; copying 147db96d56Sopenharmony_ci single: copying files 157db96d56Sopenharmony_ci 167db96d56Sopenharmony_ci-------------- 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ciThe :mod:`shutil` module offers a number of high-level operations on files and 197db96d56Sopenharmony_cicollections of files. In particular, functions are provided which support file 207db96d56Sopenharmony_cicopying and removal. For operations on individual files, see also the 217db96d56Sopenharmony_ci:mod:`os` module. 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ci.. warning:: 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci Even the higher-level file copying functions (:func:`shutil.copy`, 267db96d56Sopenharmony_ci :func:`shutil.copy2`) cannot copy all file metadata. 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ci On POSIX platforms, this means that file owner and group are lost as well 297db96d56Sopenharmony_ci as ACLs. On Mac OS, the resource fork and other metadata are not used. 307db96d56Sopenharmony_ci This means that resources will be lost and file type and creator codes will 317db96d56Sopenharmony_ci not be correct. On Windows, file owners, ACLs and alternate data streams 327db96d56Sopenharmony_ci are not copied. 337db96d56Sopenharmony_ci 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci.. _file-operations: 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ciDirectory and files operations 387db96d56Sopenharmony_ci------------------------------ 397db96d56Sopenharmony_ci 407db96d56Sopenharmony_ci.. function:: copyfileobj(fsrc, fdst[, length]) 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci Copy the contents of the file-like object *fsrc* to the file-like object *fdst*. 437db96d56Sopenharmony_ci The integer *length*, if given, is the buffer size. In particular, a negative 447db96d56Sopenharmony_ci *length* value means to copy the data without looping over the source data in 457db96d56Sopenharmony_ci chunks; by default the data is read in chunks to avoid uncontrolled memory 467db96d56Sopenharmony_ci consumption. Note that if the current file position of the *fsrc* object is not 477db96d56Sopenharmony_ci 0, only the contents from the current file position to the end of the file will 487db96d56Sopenharmony_ci be copied. 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci.. function:: copyfile(src, dst, *, follow_symlinks=True) 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ci Copy the contents (no metadata) of the file named *src* to a file named 547db96d56Sopenharmony_ci *dst* and return *dst* in the most efficient way possible. 557db96d56Sopenharmony_ci *src* and *dst* are path-like objects or path names given as strings. 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci *dst* must be the complete target file name; look at :func:`~shutil.copy` 587db96d56Sopenharmony_ci for a copy that accepts a target directory path. If *src* and *dst* 597db96d56Sopenharmony_ci specify the same file, :exc:`SameFileError` is raised. 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci The destination location must be writable; otherwise, an :exc:`OSError` 627db96d56Sopenharmony_ci exception will be raised. If *dst* already exists, it will be replaced. 637db96d56Sopenharmony_ci Special files such as character or block devices and pipes cannot be 647db96d56Sopenharmony_ci copied with this function. 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci If *follow_symlinks* is false and *src* is a symbolic link, 677db96d56Sopenharmony_ci a new symbolic link will be created instead of copying the 687db96d56Sopenharmony_ci file *src* points to. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci .. audit-event:: shutil.copyfile src,dst shutil.copyfile 717db96d56Sopenharmony_ci 727db96d56Sopenharmony_ci .. versionchanged:: 3.3 737db96d56Sopenharmony_ci :exc:`IOError` used to be raised instead of :exc:`OSError`. 747db96d56Sopenharmony_ci Added *follow_symlinks* argument. 757db96d56Sopenharmony_ci Now returns *dst*. 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ci .. versionchanged:: 3.4 787db96d56Sopenharmony_ci Raise :exc:`SameFileError` instead of :exc:`Error`. Since the former is 797db96d56Sopenharmony_ci a subclass of the latter, this change is backward compatible. 807db96d56Sopenharmony_ci 817db96d56Sopenharmony_ci .. versionchanged:: 3.8 827db96d56Sopenharmony_ci Platform-specific fast-copy syscalls may be used internally in order to 837db96d56Sopenharmony_ci copy the file more efficiently. See 847db96d56Sopenharmony_ci :ref:`shutil-platform-dependent-efficient-copy-operations` section. 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci.. exception:: SameFileError 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ci This exception is raised if source and destination in :func:`copyfile` 897db96d56Sopenharmony_ci are the same file. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci .. versionadded:: 3.4 927db96d56Sopenharmony_ci 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci.. function:: copymode(src, dst, *, follow_symlinks=True) 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci Copy the permission bits from *src* to *dst*. The file contents, owner, and 977db96d56Sopenharmony_ci group are unaffected. *src* and *dst* are path-like objects or path names 987db96d56Sopenharmony_ci given as strings. 997db96d56Sopenharmony_ci If *follow_symlinks* is false, and both *src* and *dst* are symbolic links, 1007db96d56Sopenharmony_ci :func:`copymode` will attempt to modify the mode of *dst* itself (rather 1017db96d56Sopenharmony_ci than the file it points to). This functionality is not available on every 1027db96d56Sopenharmony_ci platform; please see :func:`copystat` for more information. If 1037db96d56Sopenharmony_ci :func:`copymode` cannot modify symbolic links on the local platform, and it 1047db96d56Sopenharmony_ci is asked to do so, it will do nothing and return. 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci .. audit-event:: shutil.copymode src,dst shutil.copymode 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci .. versionchanged:: 3.3 1097db96d56Sopenharmony_ci Added *follow_symlinks* argument. 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci.. function:: copystat(src, dst, *, follow_symlinks=True) 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci Copy the permission bits, last access time, last modification time, and 1147db96d56Sopenharmony_ci flags from *src* to *dst*. On Linux, :func:`copystat` also copies the 1157db96d56Sopenharmony_ci "extended attributes" where possible. The file contents, owner, and 1167db96d56Sopenharmony_ci group are unaffected. *src* and *dst* are path-like objects or path 1177db96d56Sopenharmony_ci names given as strings. 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci If *follow_symlinks* is false, and *src* and *dst* both 1207db96d56Sopenharmony_ci refer to symbolic links, :func:`copystat` will operate on 1217db96d56Sopenharmony_ci the symbolic links themselves rather than the files the 1227db96d56Sopenharmony_ci symbolic links refer to—reading the information from the 1237db96d56Sopenharmony_ci *src* symbolic link, and writing the information to the 1247db96d56Sopenharmony_ci *dst* symbolic link. 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ci .. note:: 1277db96d56Sopenharmony_ci 1287db96d56Sopenharmony_ci Not all platforms provide the ability to examine and 1297db96d56Sopenharmony_ci modify symbolic links. Python itself can tell you what 1307db96d56Sopenharmony_ci functionality is locally available. 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci * If ``os.chmod in os.supports_follow_symlinks`` is 1337db96d56Sopenharmony_ci ``True``, :func:`copystat` can modify the permission 1347db96d56Sopenharmony_ci bits of a symbolic link. 1357db96d56Sopenharmony_ci 1367db96d56Sopenharmony_ci * If ``os.utime in os.supports_follow_symlinks`` is 1377db96d56Sopenharmony_ci ``True``, :func:`copystat` can modify the last access 1387db96d56Sopenharmony_ci and modification times of a symbolic link. 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ci * If ``os.chflags in os.supports_follow_symlinks`` is 1417db96d56Sopenharmony_ci ``True``, :func:`copystat` can modify the flags of 1427db96d56Sopenharmony_ci a symbolic link. (``os.chflags`` is not available on 1437db96d56Sopenharmony_ci all platforms.) 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci On platforms where some or all of this functionality 1467db96d56Sopenharmony_ci is unavailable, when asked to modify a symbolic link, 1477db96d56Sopenharmony_ci :func:`copystat` will copy everything it can. 1487db96d56Sopenharmony_ci :func:`copystat` never returns failure. 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci Please see :data:`os.supports_follow_symlinks` 1517db96d56Sopenharmony_ci for more information. 1527db96d56Sopenharmony_ci 1537db96d56Sopenharmony_ci .. audit-event:: shutil.copystat src,dst shutil.copystat 1547db96d56Sopenharmony_ci 1557db96d56Sopenharmony_ci .. versionchanged:: 3.3 1567db96d56Sopenharmony_ci Added *follow_symlinks* argument and support for Linux extended attributes. 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci.. function:: copy(src, dst, *, follow_symlinks=True) 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ci Copies the file *src* to the file or directory *dst*. *src* and *dst* 1617db96d56Sopenharmony_ci should be :term:`path-like objects <path-like object>` or strings. If 1627db96d56Sopenharmony_ci *dst* specifies a directory, the file will be copied into *dst* using the 1637db96d56Sopenharmony_ci base filename from *src*. If *dst* specifies a file that already exists, 1647db96d56Sopenharmony_ci it will be replaced. Returns the path to the newly created file. 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ci If *follow_symlinks* is false, and *src* is a symbolic link, 1677db96d56Sopenharmony_ci *dst* will be created as a symbolic link. If *follow_symlinks* 1687db96d56Sopenharmony_ci is true and *src* is a symbolic link, *dst* will be a copy of 1697db96d56Sopenharmony_ci the file *src* refers to. 1707db96d56Sopenharmony_ci 1717db96d56Sopenharmony_ci :func:`~shutil.copy` copies the file data and the file's permission 1727db96d56Sopenharmony_ci mode (see :func:`os.chmod`). Other metadata, like the 1737db96d56Sopenharmony_ci file's creation and modification times, is not preserved. 1747db96d56Sopenharmony_ci To preserve all file metadata from the original, use 1757db96d56Sopenharmony_ci :func:`~shutil.copy2` instead. 1767db96d56Sopenharmony_ci 1777db96d56Sopenharmony_ci .. audit-event:: shutil.copyfile src,dst shutil.copy 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci .. audit-event:: shutil.copymode src,dst shutil.copy 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci .. versionchanged:: 3.3 1827db96d56Sopenharmony_ci Added *follow_symlinks* argument. 1837db96d56Sopenharmony_ci Now returns path to the newly created file. 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci .. versionchanged:: 3.8 1867db96d56Sopenharmony_ci Platform-specific fast-copy syscalls may be used internally in order to 1877db96d56Sopenharmony_ci copy the file more efficiently. See 1887db96d56Sopenharmony_ci :ref:`shutil-platform-dependent-efficient-copy-operations` section. 1897db96d56Sopenharmony_ci 1907db96d56Sopenharmony_ci.. function:: copy2(src, dst, *, follow_symlinks=True) 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ci Identical to :func:`~shutil.copy` except that :func:`copy2` 1937db96d56Sopenharmony_ci also attempts to preserve file metadata. 1947db96d56Sopenharmony_ci 1957db96d56Sopenharmony_ci When *follow_symlinks* is false, and *src* is a symbolic 1967db96d56Sopenharmony_ci link, :func:`copy2` attempts to copy all metadata from the 1977db96d56Sopenharmony_ci *src* symbolic link to the newly created *dst* symbolic link. 1987db96d56Sopenharmony_ci However, this functionality is not available on all platforms. 1997db96d56Sopenharmony_ci On platforms where some or all of this functionality is 2007db96d56Sopenharmony_ci unavailable, :func:`copy2` will preserve all the metadata 2017db96d56Sopenharmony_ci it can; :func:`copy2` never raises an exception because it 2027db96d56Sopenharmony_ci cannot preserve file metadata. 2037db96d56Sopenharmony_ci 2047db96d56Sopenharmony_ci :func:`copy2` uses :func:`copystat` to copy the file metadata. 2057db96d56Sopenharmony_ci Please see :func:`copystat` for more information 2067db96d56Sopenharmony_ci about platform support for modifying symbolic link metadata. 2077db96d56Sopenharmony_ci 2087db96d56Sopenharmony_ci .. audit-event:: shutil.copyfile src,dst shutil.copy2 2097db96d56Sopenharmony_ci 2107db96d56Sopenharmony_ci .. audit-event:: shutil.copystat src,dst shutil.copy2 2117db96d56Sopenharmony_ci 2127db96d56Sopenharmony_ci .. versionchanged:: 3.3 2137db96d56Sopenharmony_ci Added *follow_symlinks* argument, try to copy extended 2147db96d56Sopenharmony_ci file system attributes too (currently Linux only). 2157db96d56Sopenharmony_ci Now returns path to the newly created file. 2167db96d56Sopenharmony_ci 2177db96d56Sopenharmony_ci .. versionchanged:: 3.8 2187db96d56Sopenharmony_ci Platform-specific fast-copy syscalls may be used internally in order to 2197db96d56Sopenharmony_ci copy the file more efficiently. See 2207db96d56Sopenharmony_ci :ref:`shutil-platform-dependent-efficient-copy-operations` section. 2217db96d56Sopenharmony_ci 2227db96d56Sopenharmony_ci.. function:: ignore_patterns(*patterns) 2237db96d56Sopenharmony_ci 2247db96d56Sopenharmony_ci This factory function creates a function that can be used as a callable for 2257db96d56Sopenharmony_ci :func:`copytree`\'s *ignore* argument, ignoring files and directories that 2267db96d56Sopenharmony_ci match one of the glob-style *patterns* provided. See the example below. 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci 2297db96d56Sopenharmony_ci.. function:: copytree(src, dst, symlinks=False, ignore=None, \ 2307db96d56Sopenharmony_ci copy_function=copy2, ignore_dangling_symlinks=False, \ 2317db96d56Sopenharmony_ci dirs_exist_ok=False) 2327db96d56Sopenharmony_ci 2337db96d56Sopenharmony_ci Recursively copy an entire directory tree rooted at *src* to a directory 2347db96d56Sopenharmony_ci named *dst* and return the destination directory. All intermediate 2357db96d56Sopenharmony_ci directories needed to contain *dst* will also be created by default. 2367db96d56Sopenharmony_ci 2377db96d56Sopenharmony_ci Permissions and times of directories are copied with :func:`copystat`, 2387db96d56Sopenharmony_ci individual files are copied using :func:`~shutil.copy2`. 2397db96d56Sopenharmony_ci 2407db96d56Sopenharmony_ci If *symlinks* is true, symbolic links in the source tree are represented as 2417db96d56Sopenharmony_ci symbolic links in the new tree and the metadata of the original links will 2427db96d56Sopenharmony_ci be copied as far as the platform allows; if false or omitted, the contents 2437db96d56Sopenharmony_ci and metadata of the linked files are copied to the new tree. 2447db96d56Sopenharmony_ci 2457db96d56Sopenharmony_ci When *symlinks* is false, if the file pointed by the symlink doesn't 2467db96d56Sopenharmony_ci exist, an exception will be added in the list of errors raised in 2477db96d56Sopenharmony_ci an :exc:`Error` exception at the end of the copy process. 2487db96d56Sopenharmony_ci You can set the optional *ignore_dangling_symlinks* flag to true if you 2497db96d56Sopenharmony_ci want to silence this exception. Notice that this option has no effect 2507db96d56Sopenharmony_ci on platforms that don't support :func:`os.symlink`. 2517db96d56Sopenharmony_ci 2527db96d56Sopenharmony_ci If *ignore* is given, it must be a callable that will receive as its 2537db96d56Sopenharmony_ci arguments the directory being visited by :func:`copytree`, and a list of its 2547db96d56Sopenharmony_ci contents, as returned by :func:`os.listdir`. Since :func:`copytree` is 2557db96d56Sopenharmony_ci called recursively, the *ignore* callable will be called once for each 2567db96d56Sopenharmony_ci directory that is copied. The callable must return a sequence of directory 2577db96d56Sopenharmony_ci and file names relative to the current directory (i.e. a subset of the items 2587db96d56Sopenharmony_ci in its second argument); these names will then be ignored in the copy 2597db96d56Sopenharmony_ci process. :func:`ignore_patterns` can be used to create such a callable that 2607db96d56Sopenharmony_ci ignores names based on glob-style patterns. 2617db96d56Sopenharmony_ci 2627db96d56Sopenharmony_ci If exception(s) occur, an :exc:`Error` is raised with a list of reasons. 2637db96d56Sopenharmony_ci 2647db96d56Sopenharmony_ci If *copy_function* is given, it must be a callable that will be used to copy 2657db96d56Sopenharmony_ci each file. It will be called with the source path and the destination path 2667db96d56Sopenharmony_ci as arguments. By default, :func:`~shutil.copy2` is used, but any function 2677db96d56Sopenharmony_ci that supports the same signature (like :func:`~shutil.copy`) can be used. 2687db96d56Sopenharmony_ci 2697db96d56Sopenharmony_ci If *dirs_exist_ok* is false (the default) and *dst* already exists, a 2707db96d56Sopenharmony_ci :exc:`FileExistsError` is raised. If *dirs_exist_ok* is true, the copying 2717db96d56Sopenharmony_ci operation will continue if it encounters existing directories, and files 2727db96d56Sopenharmony_ci within the *dst* tree will be overwritten by corresponding files from the 2737db96d56Sopenharmony_ci *src* tree. 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ci .. audit-event:: shutil.copytree src,dst shutil.copytree 2767db96d56Sopenharmony_ci 2777db96d56Sopenharmony_ci .. versionchanged:: 3.3 2787db96d56Sopenharmony_ci Copy metadata when *symlinks* is false. 2797db96d56Sopenharmony_ci Now returns *dst*. 2807db96d56Sopenharmony_ci 2817db96d56Sopenharmony_ci .. versionchanged:: 3.2 2827db96d56Sopenharmony_ci Added the *copy_function* argument to be able to provide a custom copy 2837db96d56Sopenharmony_ci function. 2847db96d56Sopenharmony_ci Added the *ignore_dangling_symlinks* argument to silence dangling symlinks 2857db96d56Sopenharmony_ci errors when *symlinks* is false. 2867db96d56Sopenharmony_ci 2877db96d56Sopenharmony_ci .. versionchanged:: 3.8 2887db96d56Sopenharmony_ci Platform-specific fast-copy syscalls may be used internally in order to 2897db96d56Sopenharmony_ci copy the file more efficiently. See 2907db96d56Sopenharmony_ci :ref:`shutil-platform-dependent-efficient-copy-operations` section. 2917db96d56Sopenharmony_ci 2927db96d56Sopenharmony_ci .. versionadded:: 3.8 2937db96d56Sopenharmony_ci The *dirs_exist_ok* parameter. 2947db96d56Sopenharmony_ci 2957db96d56Sopenharmony_ci.. function:: rmtree(path, ignore_errors=False, onerror=None, *, dir_fd=None) 2967db96d56Sopenharmony_ci 2977db96d56Sopenharmony_ci .. index:: single: directory; deleting 2987db96d56Sopenharmony_ci 2997db96d56Sopenharmony_ci Delete an entire directory tree; *path* must point to a directory (but not a 3007db96d56Sopenharmony_ci symbolic link to a directory). If *ignore_errors* is true, errors resulting 3017db96d56Sopenharmony_ci from failed removals will be ignored; if false or omitted, such errors are 3027db96d56Sopenharmony_ci handled by calling a handler specified by *onerror* or, if that is omitted, 3037db96d56Sopenharmony_ci they raise an exception. 3047db96d56Sopenharmony_ci 3057db96d56Sopenharmony_ci This function can support :ref:`paths relative to directory descriptors 3067db96d56Sopenharmony_ci <dir_fd>`. 3077db96d56Sopenharmony_ci 3087db96d56Sopenharmony_ci .. note:: 3097db96d56Sopenharmony_ci 3107db96d56Sopenharmony_ci On platforms that support the necessary fd-based functions a symlink 3117db96d56Sopenharmony_ci attack resistant version of :func:`rmtree` is used by default. On other 3127db96d56Sopenharmony_ci platforms, the :func:`rmtree` implementation is susceptible to a symlink 3137db96d56Sopenharmony_ci attack: given proper timing and circumstances, attackers can manipulate 3147db96d56Sopenharmony_ci symlinks on the filesystem to delete files they wouldn't be able to access 3157db96d56Sopenharmony_ci otherwise. Applications can use the :data:`rmtree.avoids_symlink_attacks` 3167db96d56Sopenharmony_ci function attribute to determine which case applies. 3177db96d56Sopenharmony_ci 3187db96d56Sopenharmony_ci If *onerror* is provided, it must be a callable that accepts three 3197db96d56Sopenharmony_ci parameters: *function*, *path*, and *excinfo*. 3207db96d56Sopenharmony_ci 3217db96d56Sopenharmony_ci The first parameter, *function*, is the function which raised the exception; 3227db96d56Sopenharmony_ci it depends on the platform and implementation. The second parameter, 3237db96d56Sopenharmony_ci *path*, will be the path name passed to *function*. The third parameter, 3247db96d56Sopenharmony_ci *excinfo*, will be the exception information returned by 3257db96d56Sopenharmony_ci :func:`sys.exc_info`. Exceptions raised by *onerror* will not be caught. 3267db96d56Sopenharmony_ci 3277db96d56Sopenharmony_ci .. audit-event:: shutil.rmtree path,dir_fd shutil.rmtree 3287db96d56Sopenharmony_ci 3297db96d56Sopenharmony_ci .. versionchanged:: 3.3 3307db96d56Sopenharmony_ci Added a symlink attack resistant version that is used automatically 3317db96d56Sopenharmony_ci if platform supports fd-based functions. 3327db96d56Sopenharmony_ci 3337db96d56Sopenharmony_ci .. versionchanged:: 3.8 3347db96d56Sopenharmony_ci On Windows, will no longer delete the contents of a directory junction 3357db96d56Sopenharmony_ci before removing the junction. 3367db96d56Sopenharmony_ci 3377db96d56Sopenharmony_ci .. versionchanged:: 3.11 3387db96d56Sopenharmony_ci The *dir_fd* parameter. 3397db96d56Sopenharmony_ci 3407db96d56Sopenharmony_ci .. attribute:: rmtree.avoids_symlink_attacks 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ci Indicates whether the current platform and implementation provides a 3437db96d56Sopenharmony_ci symlink attack resistant version of :func:`rmtree`. Currently this is 3447db96d56Sopenharmony_ci only true for platforms supporting fd-based directory access functions. 3457db96d56Sopenharmony_ci 3467db96d56Sopenharmony_ci .. versionadded:: 3.3 3477db96d56Sopenharmony_ci 3487db96d56Sopenharmony_ci 3497db96d56Sopenharmony_ci.. function:: move(src, dst, copy_function=copy2) 3507db96d56Sopenharmony_ci 3517db96d56Sopenharmony_ci Recursively move a file or directory (*src*) to another location (*dst*) 3527db96d56Sopenharmony_ci and return the destination. 3537db96d56Sopenharmony_ci 3547db96d56Sopenharmony_ci If the destination is an existing directory, then *src* is moved inside that 3557db96d56Sopenharmony_ci directory. If the destination already exists but is not a directory, it may 3567db96d56Sopenharmony_ci be overwritten depending on :func:`os.rename` semantics. 3577db96d56Sopenharmony_ci 3587db96d56Sopenharmony_ci If the destination is on the current filesystem, then :func:`os.rename` is 3597db96d56Sopenharmony_ci used. Otherwise, *src* is copied to *dst* using *copy_function* and then 3607db96d56Sopenharmony_ci removed. In case of symlinks, a new symlink pointing to the target of *src* 3617db96d56Sopenharmony_ci will be created in or as *dst* and *src* will be removed. 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci If *copy_function* is given, it must be a callable that takes two arguments 3647db96d56Sopenharmony_ci *src* and *dst*, and will be used to copy *src* to *dst* if 3657db96d56Sopenharmony_ci :func:`os.rename` cannot be used. If the source is a directory, 3667db96d56Sopenharmony_ci :func:`copytree` is called, passing it the :func:`copy_function`. The 3677db96d56Sopenharmony_ci default *copy_function* is :func:`copy2`. Using :func:`~shutil.copy` as the 3687db96d56Sopenharmony_ci *copy_function* allows the move to succeed when it is not possible to also 3697db96d56Sopenharmony_ci copy the metadata, at the expense of not copying any of the metadata. 3707db96d56Sopenharmony_ci 3717db96d56Sopenharmony_ci .. audit-event:: shutil.move src,dst shutil.move 3727db96d56Sopenharmony_ci 3737db96d56Sopenharmony_ci .. versionchanged:: 3.3 3747db96d56Sopenharmony_ci Added explicit symlink handling for foreign filesystems, thus adapting 3757db96d56Sopenharmony_ci it to the behavior of GNU's :program:`mv`. 3767db96d56Sopenharmony_ci Now returns *dst*. 3777db96d56Sopenharmony_ci 3787db96d56Sopenharmony_ci .. versionchanged:: 3.5 3797db96d56Sopenharmony_ci Added the *copy_function* keyword argument. 3807db96d56Sopenharmony_ci 3817db96d56Sopenharmony_ci .. versionchanged:: 3.8 3827db96d56Sopenharmony_ci Platform-specific fast-copy syscalls may be used internally in order to 3837db96d56Sopenharmony_ci copy the file more efficiently. See 3847db96d56Sopenharmony_ci :ref:`shutil-platform-dependent-efficient-copy-operations` section. 3857db96d56Sopenharmony_ci 3867db96d56Sopenharmony_ci .. versionchanged:: 3.9 3877db96d56Sopenharmony_ci Accepts a :term:`path-like object` for both *src* and *dst*. 3887db96d56Sopenharmony_ci 3897db96d56Sopenharmony_ci.. function:: disk_usage(path) 3907db96d56Sopenharmony_ci 3917db96d56Sopenharmony_ci Return disk usage statistics about the given path as a :term:`named tuple` 3927db96d56Sopenharmony_ci with the attributes *total*, *used* and *free*, which are the amount of 3937db96d56Sopenharmony_ci total, used and free space, in bytes. *path* may be a file or a 3947db96d56Sopenharmony_ci directory. 3957db96d56Sopenharmony_ci 3967db96d56Sopenharmony_ci .. versionadded:: 3.3 3977db96d56Sopenharmony_ci 3987db96d56Sopenharmony_ci .. versionchanged:: 3.8 3997db96d56Sopenharmony_ci On Windows, *path* can now be a file or directory. 4007db96d56Sopenharmony_ci 4017db96d56Sopenharmony_ci .. availability:: Unix, Windows. 4027db96d56Sopenharmony_ci 4037db96d56Sopenharmony_ci.. function:: chown(path, user=None, group=None) 4047db96d56Sopenharmony_ci 4057db96d56Sopenharmony_ci Change owner *user* and/or *group* of the given *path*. 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ci *user* can be a system user name or a uid; the same applies to *group*. At 4087db96d56Sopenharmony_ci least one argument is required. 4097db96d56Sopenharmony_ci 4107db96d56Sopenharmony_ci See also :func:`os.chown`, the underlying function. 4117db96d56Sopenharmony_ci 4127db96d56Sopenharmony_ci .. audit-event:: shutil.chown path,user,group shutil.chown 4137db96d56Sopenharmony_ci 4147db96d56Sopenharmony_ci .. availability:: Unix. 4157db96d56Sopenharmony_ci 4167db96d56Sopenharmony_ci .. versionadded:: 3.3 4177db96d56Sopenharmony_ci 4187db96d56Sopenharmony_ci 4197db96d56Sopenharmony_ci.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None) 4207db96d56Sopenharmony_ci 4217db96d56Sopenharmony_ci Return the path to an executable which would be run if the given *cmd* was 4227db96d56Sopenharmony_ci called. If no *cmd* would be called, return ``None``. 4237db96d56Sopenharmony_ci 4247db96d56Sopenharmony_ci *mode* is a permission mask passed to :func:`os.access`, by default 4257db96d56Sopenharmony_ci determining if the file exists and executable. 4267db96d56Sopenharmony_ci 4277db96d56Sopenharmony_ci When no *path* is specified, the results of :func:`os.environ` are used, 4287db96d56Sopenharmony_ci returning either the "PATH" value or a fallback of :attr:`os.defpath`. 4297db96d56Sopenharmony_ci 4307db96d56Sopenharmony_ci On Windows, the current directory is always prepended to the *path* whether 4317db96d56Sopenharmony_ci or not you use the default or provide your own, which is the behavior the 4327db96d56Sopenharmony_ci command shell uses when finding executables. Additionally, when finding the 4337db96d56Sopenharmony_ci *cmd* in the *path*, the ``PATHEXT`` environment variable is checked. For 4347db96d56Sopenharmony_ci example, if you call ``shutil.which("python")``, :func:`which` will search 4357db96d56Sopenharmony_ci ``PATHEXT`` to know that it should look for ``python.exe`` within the *path* 4367db96d56Sopenharmony_ci directories. For example, on Windows:: 4377db96d56Sopenharmony_ci 4387db96d56Sopenharmony_ci >>> shutil.which("python") 4397db96d56Sopenharmony_ci 'C:\\Python33\\python.EXE' 4407db96d56Sopenharmony_ci 4417db96d56Sopenharmony_ci .. versionadded:: 3.3 4427db96d56Sopenharmony_ci 4437db96d56Sopenharmony_ci .. versionchanged:: 3.8 4447db96d56Sopenharmony_ci The :class:`bytes` type is now accepted. If *cmd* type is 4457db96d56Sopenharmony_ci :class:`bytes`, the result type is also :class:`bytes`. 4467db96d56Sopenharmony_ci 4477db96d56Sopenharmony_ci.. exception:: Error 4487db96d56Sopenharmony_ci 4497db96d56Sopenharmony_ci This exception collects exceptions that are raised during a multi-file 4507db96d56Sopenharmony_ci operation. For :func:`copytree`, the exception argument is a list of 3-tuples 4517db96d56Sopenharmony_ci (*srcname*, *dstname*, *exception*). 4527db96d56Sopenharmony_ci 4537db96d56Sopenharmony_ci.. _shutil-platform-dependent-efficient-copy-operations: 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ciPlatform-dependent efficient copy operations 4567db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4577db96d56Sopenharmony_ci 4587db96d56Sopenharmony_ciStarting from Python 3.8, all functions involving a file copy 4597db96d56Sopenharmony_ci(:func:`copyfile`, :func:`~shutil.copy`, :func:`copy2`, 4607db96d56Sopenharmony_ci:func:`copytree`, and :func:`move`) may use 4617db96d56Sopenharmony_ciplatform-specific "fast-copy" syscalls in order to copy the file more 4627db96d56Sopenharmony_ciefficiently (see :issue:`33671`). 4637db96d56Sopenharmony_ci"fast-copy" means that the copying operation occurs within the kernel, avoiding 4647db96d56Sopenharmony_cithe use of userspace buffers in Python as in "``outfd.write(infd.read())``". 4657db96d56Sopenharmony_ci 4667db96d56Sopenharmony_ciOn macOS `fcopyfile`_ is used to copy the file content (not metadata). 4677db96d56Sopenharmony_ci 4687db96d56Sopenharmony_ciOn Linux :func:`os.sendfile` is used. 4697db96d56Sopenharmony_ci 4707db96d56Sopenharmony_ciOn Windows :func:`shutil.copyfile` uses a bigger default buffer size (1 MiB 4717db96d56Sopenharmony_ciinstead of 64 KiB) and a :func:`memoryview`-based variant of 4727db96d56Sopenharmony_ci:func:`shutil.copyfileobj` is used. 4737db96d56Sopenharmony_ci 4747db96d56Sopenharmony_ciIf the fast-copy operation fails and no data was written in the destination 4757db96d56Sopenharmony_cifile then shutil will silently fallback on using less efficient 4767db96d56Sopenharmony_ci:func:`copyfileobj` function internally. 4777db96d56Sopenharmony_ci 4787db96d56Sopenharmony_ci.. versionchanged:: 3.8 4797db96d56Sopenharmony_ci 4807db96d56Sopenharmony_ci.. _shutil-copytree-example: 4817db96d56Sopenharmony_ci 4827db96d56Sopenharmony_cicopytree example 4837db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~ 4847db96d56Sopenharmony_ci 4857db96d56Sopenharmony_ciAn example that uses the :func:`ignore_patterns` helper:: 4867db96d56Sopenharmony_ci 4877db96d56Sopenharmony_ci from shutil import copytree, ignore_patterns 4887db96d56Sopenharmony_ci 4897db96d56Sopenharmony_ci copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*')) 4907db96d56Sopenharmony_ci 4917db96d56Sopenharmony_ciThis will copy everything except ``.pyc`` files and files or directories whose 4927db96d56Sopenharmony_ciname starts with ``tmp``. 4937db96d56Sopenharmony_ci 4947db96d56Sopenharmony_ciAnother example that uses the *ignore* argument to add a logging call:: 4957db96d56Sopenharmony_ci 4967db96d56Sopenharmony_ci from shutil import copytree 4977db96d56Sopenharmony_ci import logging 4987db96d56Sopenharmony_ci 4997db96d56Sopenharmony_ci def _logpath(path, names): 5007db96d56Sopenharmony_ci logging.info('Working in %s', path) 5017db96d56Sopenharmony_ci return [] # nothing will be ignored 5027db96d56Sopenharmony_ci 5037db96d56Sopenharmony_ci copytree(source, destination, ignore=_logpath) 5047db96d56Sopenharmony_ci 5057db96d56Sopenharmony_ci 5067db96d56Sopenharmony_ci.. _shutil-rmtree-example: 5077db96d56Sopenharmony_ci 5087db96d56Sopenharmony_cirmtree example 5097db96d56Sopenharmony_ci~~~~~~~~~~~~~~ 5107db96d56Sopenharmony_ci 5117db96d56Sopenharmony_ciThis example shows how to remove a directory tree on Windows where some 5127db96d56Sopenharmony_ciof the files have their read-only bit set. It uses the onerror callback 5137db96d56Sopenharmony_cito clear the readonly bit and reattempt the remove. Any subsequent failure 5147db96d56Sopenharmony_ciwill propagate. :: 5157db96d56Sopenharmony_ci 5167db96d56Sopenharmony_ci import os, stat 5177db96d56Sopenharmony_ci import shutil 5187db96d56Sopenharmony_ci 5197db96d56Sopenharmony_ci def remove_readonly(func, path, _): 5207db96d56Sopenharmony_ci "Clear the readonly bit and reattempt the removal" 5217db96d56Sopenharmony_ci os.chmod(path, stat.S_IWRITE) 5227db96d56Sopenharmony_ci func(path) 5237db96d56Sopenharmony_ci 5247db96d56Sopenharmony_ci shutil.rmtree(directory, onerror=remove_readonly) 5257db96d56Sopenharmony_ci 5267db96d56Sopenharmony_ci.. _archiving-operations: 5277db96d56Sopenharmony_ci 5287db96d56Sopenharmony_ciArchiving operations 5297db96d56Sopenharmony_ci-------------------- 5307db96d56Sopenharmony_ci 5317db96d56Sopenharmony_ci.. versionadded:: 3.2 5327db96d56Sopenharmony_ci 5337db96d56Sopenharmony_ci.. versionchanged:: 3.5 5347db96d56Sopenharmony_ci Added support for the *xztar* format. 5357db96d56Sopenharmony_ci 5367db96d56Sopenharmony_ci 5377db96d56Sopenharmony_ciHigh-level utilities to create and read compressed and archived files are also 5387db96d56Sopenharmony_ciprovided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. 5397db96d56Sopenharmony_ci 5407db96d56Sopenharmony_ci.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]]) 5417db96d56Sopenharmony_ci 5427db96d56Sopenharmony_ci Create an archive file (such as zip or tar) and return its name. 5437db96d56Sopenharmony_ci 5447db96d56Sopenharmony_ci *base_name* is the name of the file to create, including the path, minus 5457db96d56Sopenharmony_ci any format-specific extension. *format* is the archive format: one of 5467db96d56Sopenharmony_ci "zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the 5477db96d56Sopenharmony_ci :mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is 5487db96d56Sopenharmony_ci available), or "xztar" (if the :mod:`lzma` module is available). 5497db96d56Sopenharmony_ci 5507db96d56Sopenharmony_ci *root_dir* is a directory that will be the root directory of the 5517db96d56Sopenharmony_ci archive, all paths in the archive will be relative to it; for example, 5527db96d56Sopenharmony_ci we typically chdir into *root_dir* before creating the archive. 5537db96d56Sopenharmony_ci 5547db96d56Sopenharmony_ci *base_dir* is the directory where we start archiving from; 5557db96d56Sopenharmony_ci i.e. *base_dir* will be the common prefix of all files and 5567db96d56Sopenharmony_ci directories in the archive. *base_dir* must be given relative 5577db96d56Sopenharmony_ci to *root_dir*. See :ref:`shutil-archiving-example-with-basedir` for how to 5587db96d56Sopenharmony_ci use *base_dir* and *root_dir* together. 5597db96d56Sopenharmony_ci 5607db96d56Sopenharmony_ci *root_dir* and *base_dir* both default to the current directory. 5617db96d56Sopenharmony_ci 5627db96d56Sopenharmony_ci If *dry_run* is true, no archive is created, but the operations that would be 5637db96d56Sopenharmony_ci executed are logged to *logger*. 5647db96d56Sopenharmony_ci 5657db96d56Sopenharmony_ci *owner* and *group* are used when creating a tar archive. By default, 5667db96d56Sopenharmony_ci uses the current owner and group. 5677db96d56Sopenharmony_ci 5687db96d56Sopenharmony_ci *logger* must be an object compatible with :pep:`282`, usually an instance of 5697db96d56Sopenharmony_ci :class:`logging.Logger`. 5707db96d56Sopenharmony_ci 5717db96d56Sopenharmony_ci The *verbose* argument is unused and deprecated. 5727db96d56Sopenharmony_ci 5737db96d56Sopenharmony_ci .. audit-event:: shutil.make_archive base_name,format,root_dir,base_dir shutil.make_archive 5747db96d56Sopenharmony_ci 5757db96d56Sopenharmony_ci .. note:: 5767db96d56Sopenharmony_ci 5777db96d56Sopenharmony_ci This function is not thread-safe when custom archivers registered 5787db96d56Sopenharmony_ci with :func:`register_archive_format` are used. In this case it 5797db96d56Sopenharmony_ci temporarily changes the current working directory of the process 5807db96d56Sopenharmony_ci to perform archiving. 5817db96d56Sopenharmony_ci 5827db96d56Sopenharmony_ci .. versionchanged:: 3.8 5837db96d56Sopenharmony_ci The modern pax (POSIX.1-2001) format is now used instead of 5847db96d56Sopenharmony_ci the legacy GNU format for archives created with ``format="tar"``. 5857db96d56Sopenharmony_ci 5867db96d56Sopenharmony_ci .. versionchanged:: 3.10.6 5877db96d56Sopenharmony_ci This function is now made thread-safe during creation of standard 5887db96d56Sopenharmony_ci ``.zip`` and tar archives. 5897db96d56Sopenharmony_ci 5907db96d56Sopenharmony_ci.. function:: get_archive_formats() 5917db96d56Sopenharmony_ci 5927db96d56Sopenharmony_ci Return a list of supported formats for archiving. 5937db96d56Sopenharmony_ci Each element of the returned sequence is a tuple ``(name, description)``. 5947db96d56Sopenharmony_ci 5957db96d56Sopenharmony_ci By default :mod:`shutil` provides these formats: 5967db96d56Sopenharmony_ci 5977db96d56Sopenharmony_ci - *zip*: ZIP file (if the :mod:`zlib` module is available). 5987db96d56Sopenharmony_ci - *tar*: Uncompressed tar file. Uses POSIX.1-2001 pax format for new archives. 5997db96d56Sopenharmony_ci - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available). 6007db96d56Sopenharmony_ci - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available). 6017db96d56Sopenharmony_ci - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available). 6027db96d56Sopenharmony_ci 6037db96d56Sopenharmony_ci You can register new formats or provide your own archiver for any existing 6047db96d56Sopenharmony_ci formats, by using :func:`register_archive_format`. 6057db96d56Sopenharmony_ci 6067db96d56Sopenharmony_ci 6077db96d56Sopenharmony_ci.. function:: register_archive_format(name, function, [extra_args, [description]]) 6087db96d56Sopenharmony_ci 6097db96d56Sopenharmony_ci Register an archiver for the format *name*. 6107db96d56Sopenharmony_ci 6117db96d56Sopenharmony_ci *function* is the callable that will be used to unpack archives. The callable 6127db96d56Sopenharmony_ci will receive the *base_name* of the file to create, followed by the 6137db96d56Sopenharmony_ci *base_dir* (which defaults to :data:`os.curdir`) to start archiving from. 6147db96d56Sopenharmony_ci Further arguments are passed as keyword arguments: *owner*, *group*, 6157db96d56Sopenharmony_ci *dry_run* and *logger* (as passed in :func:`make_archive`). 6167db96d56Sopenharmony_ci 6177db96d56Sopenharmony_ci If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be 6187db96d56Sopenharmony_ci used as extra keywords arguments when the archiver callable is used. 6197db96d56Sopenharmony_ci 6207db96d56Sopenharmony_ci *description* is used by :func:`get_archive_formats` which returns the 6217db96d56Sopenharmony_ci list of archivers. Defaults to an empty string. 6227db96d56Sopenharmony_ci 6237db96d56Sopenharmony_ci 6247db96d56Sopenharmony_ci.. function:: unregister_archive_format(name) 6257db96d56Sopenharmony_ci 6267db96d56Sopenharmony_ci Remove the archive format *name* from the list of supported formats. 6277db96d56Sopenharmony_ci 6287db96d56Sopenharmony_ci 6297db96d56Sopenharmony_ci.. function:: unpack_archive(filename[, extract_dir[, format[, filter]]]) 6307db96d56Sopenharmony_ci 6317db96d56Sopenharmony_ci Unpack an archive. *filename* is the full path of the archive. 6327db96d56Sopenharmony_ci 6337db96d56Sopenharmony_ci *extract_dir* is the name of the target directory where the archive is 6347db96d56Sopenharmony_ci unpacked. If not provided, the current working directory is used. 6357db96d56Sopenharmony_ci 6367db96d56Sopenharmony_ci *format* is the archive format: one of "zip", "tar", "gztar", "bztar", or 6377db96d56Sopenharmony_ci "xztar". Or any other format registered with 6387db96d56Sopenharmony_ci :func:`register_unpack_format`. If not provided, :func:`unpack_archive` 6397db96d56Sopenharmony_ci will use the archive file name extension and see if an unpacker was 6407db96d56Sopenharmony_ci registered for that extension. In case none is found, 6417db96d56Sopenharmony_ci a :exc:`ValueError` is raised. 6427db96d56Sopenharmony_ci 6437db96d56Sopenharmony_ci The keyword-only *filter* argument, which was added in Python 3.11.4, 6447db96d56Sopenharmony_ci is passed to the underlying unpacking function. 6457db96d56Sopenharmony_ci For zip files, *filter* is not accepted. 6467db96d56Sopenharmony_ci For tar files, it is recommended to set it to ``'data'``, 6477db96d56Sopenharmony_ci unless using features specific to tar and UNIX-like filesystems. 6487db96d56Sopenharmony_ci (See :ref:`tarfile-extraction-filter` for details.) 6497db96d56Sopenharmony_ci The ``'data'`` filter will become the default for tar files 6507db96d56Sopenharmony_ci in Python 3.14. 6517db96d56Sopenharmony_ci 6527db96d56Sopenharmony_ci .. audit-event:: shutil.unpack_archive filename,extract_dir,format shutil.unpack_archive 6537db96d56Sopenharmony_ci 6547db96d56Sopenharmony_ci .. warning:: 6557db96d56Sopenharmony_ci 6567db96d56Sopenharmony_ci Never extract archives from untrusted sources without prior inspection. 6577db96d56Sopenharmony_ci It is possible that files are created outside of the path specified in 6587db96d56Sopenharmony_ci the *extract_dir* argument, e.g. members that have absolute filenames 6597db96d56Sopenharmony_ci starting with "/" or filenames with two dots "..". 6607db96d56Sopenharmony_ci 6617db96d56Sopenharmony_ci .. versionchanged:: 3.7 6627db96d56Sopenharmony_ci Accepts a :term:`path-like object` for *filename* and *extract_dir*. 6637db96d56Sopenharmony_ci 6647db96d56Sopenharmony_ci .. versionchanged:: 3.11.4 6657db96d56Sopenharmony_ci Added the *filter* argument. 6667db96d56Sopenharmony_ci 6677db96d56Sopenharmony_ci.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]]) 6687db96d56Sopenharmony_ci 6697db96d56Sopenharmony_ci Registers an unpack format. *name* is the name of the format and 6707db96d56Sopenharmony_ci *extensions* is a list of extensions corresponding to the format, like 6717db96d56Sopenharmony_ci ``.zip`` for Zip files. 6727db96d56Sopenharmony_ci 6737db96d56Sopenharmony_ci *function* is the callable that will be used to unpack archives. The 6747db96d56Sopenharmony_ci callable will receive: 6757db96d56Sopenharmony_ci 6767db96d56Sopenharmony_ci - the path of the archive, as a positional argument; 6777db96d56Sopenharmony_ci - the directory the archive must be extracted to, as a positional argument; 6787db96d56Sopenharmony_ci - possibly a *filter* keyword argument, if it was given to 6797db96d56Sopenharmony_ci :func:`unpack_archive`; 6807db96d56Sopenharmony_ci - additional keyword arguments, specified by *extra_args* as a sequence 6817db96d56Sopenharmony_ci of ``(name, value)`` tuples. 6827db96d56Sopenharmony_ci 6837db96d56Sopenharmony_ci *description* can be provided to describe the format, and will be returned 6847db96d56Sopenharmony_ci by the :func:`get_unpack_formats` function. 6857db96d56Sopenharmony_ci 6867db96d56Sopenharmony_ci 6877db96d56Sopenharmony_ci.. function:: unregister_unpack_format(name) 6887db96d56Sopenharmony_ci 6897db96d56Sopenharmony_ci Unregister an unpack format. *name* is the name of the format. 6907db96d56Sopenharmony_ci 6917db96d56Sopenharmony_ci 6927db96d56Sopenharmony_ci.. function:: get_unpack_formats() 6937db96d56Sopenharmony_ci 6947db96d56Sopenharmony_ci Return a list of all registered formats for unpacking. 6957db96d56Sopenharmony_ci Each element of the returned sequence is a tuple 6967db96d56Sopenharmony_ci ``(name, extensions, description)``. 6977db96d56Sopenharmony_ci 6987db96d56Sopenharmony_ci By default :mod:`shutil` provides these formats: 6997db96d56Sopenharmony_ci 7007db96d56Sopenharmony_ci - *zip*: ZIP file (unpacking compressed files works only if the corresponding 7017db96d56Sopenharmony_ci module is available). 7027db96d56Sopenharmony_ci - *tar*: uncompressed tar file. 7037db96d56Sopenharmony_ci - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available). 7047db96d56Sopenharmony_ci - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available). 7057db96d56Sopenharmony_ci - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available). 7067db96d56Sopenharmony_ci 7077db96d56Sopenharmony_ci You can register new formats or provide your own unpacker for any existing 7087db96d56Sopenharmony_ci formats, by using :func:`register_unpack_format`. 7097db96d56Sopenharmony_ci 7107db96d56Sopenharmony_ci 7117db96d56Sopenharmony_ci.. _shutil-archiving-example: 7127db96d56Sopenharmony_ci 7137db96d56Sopenharmony_ciArchiving example 7147db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~~ 7157db96d56Sopenharmony_ci 7167db96d56Sopenharmony_ciIn this example, we create a gzip'ed tar-file archive containing all files 7177db96d56Sopenharmony_cifound in the :file:`.ssh` directory of the user:: 7187db96d56Sopenharmony_ci 7197db96d56Sopenharmony_ci >>> from shutil import make_archive 7207db96d56Sopenharmony_ci >>> import os 7217db96d56Sopenharmony_ci >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) 7227db96d56Sopenharmony_ci >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh')) 7237db96d56Sopenharmony_ci >>> make_archive(archive_name, 'gztar', root_dir) 7247db96d56Sopenharmony_ci '/Users/tarek/myarchive.tar.gz' 7257db96d56Sopenharmony_ci 7267db96d56Sopenharmony_ciThe resulting archive contains: 7277db96d56Sopenharmony_ci 7287db96d56Sopenharmony_ci.. code-block:: shell-session 7297db96d56Sopenharmony_ci 7307db96d56Sopenharmony_ci $ tar -tzvf /Users/tarek/myarchive.tar.gz 7317db96d56Sopenharmony_ci drwx------ tarek/staff 0 2010-02-01 16:23:40 ./ 7327db96d56Sopenharmony_ci -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys 7337db96d56Sopenharmony_ci -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config 7347db96d56Sopenharmony_ci -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa 7357db96d56Sopenharmony_ci -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub 7367db96d56Sopenharmony_ci -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa 7377db96d56Sopenharmony_ci -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub 7387db96d56Sopenharmony_ci -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts 7397db96d56Sopenharmony_ci 7407db96d56Sopenharmony_ci 7417db96d56Sopenharmony_ci.. _shutil-archiving-example-with-basedir: 7427db96d56Sopenharmony_ci 7437db96d56Sopenharmony_ciArchiving example with *base_dir* 7447db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7457db96d56Sopenharmony_ci 7467db96d56Sopenharmony_ciIn this example, similar to the `one above <shutil-archiving-example_>`_, 7477db96d56Sopenharmony_ciwe show how to use :func:`make_archive`, but this time with the usage of 7487db96d56Sopenharmony_ci*base_dir*. We now have the following directory structure: 7497db96d56Sopenharmony_ci 7507db96d56Sopenharmony_ci.. code-block:: shell-session 7517db96d56Sopenharmony_ci 7527db96d56Sopenharmony_ci $ tree tmp 7537db96d56Sopenharmony_ci tmp 7547db96d56Sopenharmony_ci └── root 7557db96d56Sopenharmony_ci └── structure 7567db96d56Sopenharmony_ci ├── content 7577db96d56Sopenharmony_ci └── please_add.txt 7587db96d56Sopenharmony_ci └── do_not_add.txt 7597db96d56Sopenharmony_ci 7607db96d56Sopenharmony_ciIn the final archive, :file:`please_add.txt` should be included, but 7617db96d56Sopenharmony_ci:file:`do_not_add.txt` should not. Therefore we use the following:: 7627db96d56Sopenharmony_ci 7637db96d56Sopenharmony_ci >>> from shutil import make_archive 7647db96d56Sopenharmony_ci >>> import os 7657db96d56Sopenharmony_ci >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) 7667db96d56Sopenharmony_ci >>> make_archive( 7677db96d56Sopenharmony_ci ... archive_name, 7687db96d56Sopenharmony_ci ... 'tar', 7697db96d56Sopenharmony_ci ... root_dir='tmp/root', 7707db96d56Sopenharmony_ci ... base_dir='structure/content', 7717db96d56Sopenharmony_ci ... ) 7727db96d56Sopenharmony_ci '/Users/tarek/my_archive.tar' 7737db96d56Sopenharmony_ci 7747db96d56Sopenharmony_ciListing the files in the resulting archive gives us: 7757db96d56Sopenharmony_ci 7767db96d56Sopenharmony_ci.. code-block:: shell-session 7777db96d56Sopenharmony_ci 7787db96d56Sopenharmony_ci $ python -m tarfile -l /Users/tarek/myarchive.tar 7797db96d56Sopenharmony_ci structure/content/ 7807db96d56Sopenharmony_ci structure/content/please_add.txt 7817db96d56Sopenharmony_ci 7827db96d56Sopenharmony_ci 7837db96d56Sopenharmony_ciQuerying the size of the output terminal 7847db96d56Sopenharmony_ci---------------------------------------- 7857db96d56Sopenharmony_ci 7867db96d56Sopenharmony_ci.. function:: get_terminal_size(fallback=(columns, lines)) 7877db96d56Sopenharmony_ci 7887db96d56Sopenharmony_ci Get the size of the terminal window. 7897db96d56Sopenharmony_ci 7907db96d56Sopenharmony_ci For each of the two dimensions, the environment variable, ``COLUMNS`` 7917db96d56Sopenharmony_ci and ``LINES`` respectively, is checked. If the variable is defined and 7927db96d56Sopenharmony_ci the value is a positive integer, it is used. 7937db96d56Sopenharmony_ci 7947db96d56Sopenharmony_ci When ``COLUMNS`` or ``LINES`` is not defined, which is the common case, 7957db96d56Sopenharmony_ci the terminal connected to :data:`sys.__stdout__` is queried 7967db96d56Sopenharmony_ci by invoking :func:`os.get_terminal_size`. 7977db96d56Sopenharmony_ci 7987db96d56Sopenharmony_ci If the terminal size cannot be successfully queried, either because 7997db96d56Sopenharmony_ci the system doesn't support querying, or because we are not 8007db96d56Sopenharmony_ci connected to a terminal, the value given in ``fallback`` parameter 8017db96d56Sopenharmony_ci is used. ``fallback`` defaults to ``(80, 24)`` which is the default 8027db96d56Sopenharmony_ci size used by many terminal emulators. 8037db96d56Sopenharmony_ci 8047db96d56Sopenharmony_ci The value returned is a named tuple of type :class:`os.terminal_size`. 8057db96d56Sopenharmony_ci 8067db96d56Sopenharmony_ci See also: The Single UNIX Specification, Version 2, 8077db96d56Sopenharmony_ci `Other Environment Variables`_. 8087db96d56Sopenharmony_ci 8097db96d56Sopenharmony_ci .. versionadded:: 3.3 8107db96d56Sopenharmony_ci 8117db96d56Sopenharmony_ci .. versionchanged:: 3.11 8127db96d56Sopenharmony_ci The ``fallback`` values are also used if :func:`os.get_terminal_size` 8137db96d56Sopenharmony_ci returns zeroes. 8147db96d56Sopenharmony_ci 8157db96d56Sopenharmony_ci.. _`fcopyfile`: 8167db96d56Sopenharmony_ci http://www.manpagez.com/man/3/copyfile/ 8177db96d56Sopenharmony_ci 8187db96d56Sopenharmony_ci.. _`Other Environment Variables`: 8197db96d56Sopenharmony_ci https://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003 820