2007-08-15 11:28:22 -03:00
|
|
|
|
:mod:`shutil` --- High-level file operations
|
|
|
|
|
============================================
|
|
|
|
|
|
|
|
|
|
.. module:: shutil
|
|
|
|
|
:synopsis: High-level file operations, including copying.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
|
.. partly based on the docstrings
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
|
**Source code:** :source:`Lib/shutil.py`
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. index::
|
|
|
|
|
single: file; copying
|
|
|
|
|
single: copying files
|
|
|
|
|
|
2011-01-10 15:54:11 -04:00
|
|
|
|
--------------
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
The :mod:`shutil` module offers a number of high-level operations on files and
|
|
|
|
|
collections of files. In particular, functions are provided which support file
|
Merged revisions 58742-58816 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r58745 | georg.brandl | 2007-11-01 10:19:33 -0700 (Thu, 01 Nov 2007) | 2 lines
#1364: os.lstat is available on Windows too, as an alias to os.stat.
........
r58750 | christian.heimes | 2007-11-01 12:48:10 -0700 (Thu, 01 Nov 2007) | 1 line
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
........
r58751 | christian.heimes | 2007-11-01 13:11:06 -0700 (Thu, 01 Nov 2007) | 1 line
Removed non ASCII text from test as requested by Guido. Sorry :/
........
r58753 | georg.brandl | 2007-11-01 13:37:02 -0700 (Thu, 01 Nov 2007) | 2 lines
Fix markup glitch.
........
r58757 | gregory.p.smith | 2007-11-01 14:08:14 -0700 (Thu, 01 Nov 2007) | 4 lines
Fix bug introduced in revision 58385. Database keys could no longer
have NULL bytes in them. Replace the errant strdup with a
malloc+memcpy. Adds a unit test for the correct behavior.
........
r58758 | gregory.p.smith | 2007-11-01 14:15:36 -0700 (Thu, 01 Nov 2007) | 3 lines
Undo revision 58533 58534 fixes. Those were a workaround for
a problem introduced by 58385.
........
r58759 | gregory.p.smith | 2007-11-01 14:17:47 -0700 (Thu, 01 Nov 2007) | 2 lines
false "fix" undone as correct problem was found and fixed.
........
r58765 | mark.summerfield | 2007-11-02 01:24:59 -0700 (Fri, 02 Nov 2007) | 3 lines
Added more file-handling related cross-references.
........
r58766 | nick.coghlan | 2007-11-02 03:09:12 -0700 (Fri, 02 Nov 2007) | 1 line
Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate)
........
r58784 | thomas.heller | 2007-11-02 12:10:24 -0700 (Fri, 02 Nov 2007) | 4 lines
Issue #1292: On alpha, arm, ppc, and s390 linux systems the
--with-system-ffi configure option defaults to "yes" because the
bundled libffi sources are too old.
........
r58785 | thomas.heller | 2007-11-02 12:11:23 -0700 (Fri, 02 Nov 2007) | 1 line
Enable the full ctypes c_longdouble tests again.
........
r58796 | georg.brandl | 2007-11-02 13:06:17 -0700 (Fri, 02 Nov 2007) | 4 lines
Make "hashable" a glossary entry and clarify docs on __cmp__, __eq__ and __hash__.
I hope the concept of hashability is better understandable now.
Thanks to Tim Hatch for pointing out the flaws here.
........
2007-11-02 20:46:40 -03:00
|
|
|
|
copying and removal. For operations on individual files, see also the
|
|
|
|
|
:mod:`os` module.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2007-08-16 21:24:54 -03:00
|
|
|
|
.. warning::
|
Merged revisions 59774-59783 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59774 | georg.brandl | 2008-01-06 16:41:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1501: document that 0**0 == 1.
........
r59775 | georg.brandl | 2008-01-06 16:48:20 +0100 (Sun, 06 Jan 2008) | 2 lines
#759525: document that dir() doesn't return metaclass attrs when given a class as arg.
........
r59776 | georg.brandl | 2008-01-06 16:55:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1615275: clarify return object types of different tempfile factories.
........
r59777 | georg.brandl | 2008-01-06 17:01:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1727024: document that Popen.returncode is set by Popen.poll/wait.
........
r59778 | georg.brandl | 2008-01-06 17:04:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1686390: add example for csv.Sniffer use.
........
r59779 | georg.brandl | 2008-01-06 17:12:39 +0100 (Sun, 06 Jan 2008) | 2 lines
#1559684: document that shutil.copy* doesn't copy all metadata on Posix and Windows too.
........
r59780 | georg.brandl | 2008-01-06 17:17:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1582: document __reversed__, patch by Mark Russell.
........
r59781 | georg.brandl | 2008-01-06 17:22:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1499: Document compile() exceptions.
........
r59782 | georg.brandl | 2008-01-06 17:49:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
........
2008-01-06 13:05:40 -04:00
|
|
|
|
|
2012-02-13 11:30:47 -04:00
|
|
|
|
Even the higher-level file copying functions (:func:`shutil.copy`,
|
|
|
|
|
:func:`shutil.copy2`) cannot copy all file metadata.
|
2009-01-03 17:18:54 -04:00
|
|
|
|
|
Merged revisions 59774-59783 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59774 | georg.brandl | 2008-01-06 16:41:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1501: document that 0**0 == 1.
........
r59775 | georg.brandl | 2008-01-06 16:48:20 +0100 (Sun, 06 Jan 2008) | 2 lines
#759525: document that dir() doesn't return metaclass attrs when given a class as arg.
........
r59776 | georg.brandl | 2008-01-06 16:55:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1615275: clarify return object types of different tempfile factories.
........
r59777 | georg.brandl | 2008-01-06 17:01:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1727024: document that Popen.returncode is set by Popen.poll/wait.
........
r59778 | georg.brandl | 2008-01-06 17:04:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1686390: add example for csv.Sniffer use.
........
r59779 | georg.brandl | 2008-01-06 17:12:39 +0100 (Sun, 06 Jan 2008) | 2 lines
#1559684: document that shutil.copy* doesn't copy all metadata on Posix and Windows too.
........
r59780 | georg.brandl | 2008-01-06 17:17:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1582: document __reversed__, patch by Mark Russell.
........
r59781 | georg.brandl | 2008-01-06 17:22:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1499: Document compile() exceptions.
........
r59782 | georg.brandl | 2008-01-06 17:49:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
........
2008-01-06 13:05:40 -04:00
|
|
|
|
On POSIX platforms, this means that file owner and group are lost as well
|
2008-09-13 14:46:05 -03:00
|
|
|
|
as ACLs. On Mac OS, the resource fork and other metadata are not used.
|
Merged revisions 59774-59783 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59774 | georg.brandl | 2008-01-06 16:41:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1501: document that 0**0 == 1.
........
r59775 | georg.brandl | 2008-01-06 16:48:20 +0100 (Sun, 06 Jan 2008) | 2 lines
#759525: document that dir() doesn't return metaclass attrs when given a class as arg.
........
r59776 | georg.brandl | 2008-01-06 16:55:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1615275: clarify return object types of different tempfile factories.
........
r59777 | georg.brandl | 2008-01-06 17:01:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1727024: document that Popen.returncode is set by Popen.poll/wait.
........
r59778 | georg.brandl | 2008-01-06 17:04:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1686390: add example for csv.Sniffer use.
........
r59779 | georg.brandl | 2008-01-06 17:12:39 +0100 (Sun, 06 Jan 2008) | 2 lines
#1559684: document that shutil.copy* doesn't copy all metadata on Posix and Windows too.
........
r59780 | georg.brandl | 2008-01-06 17:17:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1582: document __reversed__, patch by Mark Russell.
........
r59781 | georg.brandl | 2008-01-06 17:22:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1499: Document compile() exceptions.
........
r59782 | georg.brandl | 2008-01-06 17:49:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
........
2008-01-06 13:05:40 -04:00
|
|
|
|
This means that resources will be lost and file type and creator codes will
|
|
|
|
|
not be correct. On Windows, file owners, ACLs and alternate data streams
|
|
|
|
|
are not copied.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-11-16 15:13:50 -04:00
|
|
|
|
|
2012-01-16 11:55:55 -04:00
|
|
|
|
.. _file-operations:
|
|
|
|
|
|
2010-02-23 01:30:31 -04:00
|
|
|
|
Directory and files operations
|
|
|
|
|
------------------------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. function:: copyfileobj(fsrc, fdst[, length])
|
|
|
|
|
|
|
|
|
|
Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
|
|
|
|
|
The integer *length*, if given, is the buffer size. In particular, a negative
|
|
|
|
|
*length* value means to copy the data without looping over the source data in
|
|
|
|
|
chunks; by default the data is read in chunks to avoid uncontrolled memory
|
|
|
|
|
consumption. Note that if the current file position of the *fsrc* object is not
|
|
|
|
|
0, only the contents from the current file position to the end of the file will
|
|
|
|
|
be copied.
|
|
|
|
|
|
|
|
|
|
|
2012-07-15 14:57:38 -03:00
|
|
|
|
.. function:: copyfile(src, dst, *, follow_symlinks=True)
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
|
|
2012-02-13 11:30:47 -04:00
|
|
|
|
Copy the contents (no metadata) of the file named *src* to a file named
|
2018-06-12 18:04:50 -03:00
|
|
|
|
*dst* and return *dst* in the most efficient way possible.
|
2019-09-09 12:51:56 -03:00
|
|
|
|
*src* and *dst* are path-like objects or path names given as strings.
|
2018-06-12 18:04:50 -03:00
|
|
|
|
|
2019-05-10 23:51:45 -03:00
|
|
|
|
*dst* must be the complete target file name; look at :func:`~shutil.copy`
|
2012-09-21 14:12:14 -03:00
|
|
|
|
for a copy that accepts a target directory path. If *src* and *dst*
|
2012-10-07 07:49:58 -03:00
|
|
|
|
specify the same file, :exc:`SameFileError` is raised.
|
2012-02-13 11:35:44 -04:00
|
|
|
|
|
2012-09-21 14:12:14 -03:00
|
|
|
|
The destination location must be writable; otherwise, an :exc:`OSError`
|
|
|
|
|
exception will be raised. If *dst* already exists, it will be replaced.
|
|
|
|
|
Special files such as character or block devices and pipes cannot be
|
|
|
|
|
copied with this function.
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
|
|
2012-07-15 20:58:29 -03:00
|
|
|
|
If *follow_symlinks* is false and *src* is a symbolic link,
|
|
|
|
|
a new symbolic link will be created instead of copying the
|
|
|
|
|
file *src* points to.
|
2011-12-29 13:54:15 -04:00
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.copyfile src,dst shutil.copyfile
|
|
|
|
|
|
2011-10-12 15:10:51 -03:00
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
|
:exc:`IOError` used to be raised instead of :exc:`OSError`.
|
2012-07-15 20:58:29 -03:00
|
|
|
|
Added *follow_symlinks* argument.
|
|
|
|
|
Now returns *dst*.
|
2011-10-12 15:10:51 -03:00
|
|
|
|
|
2012-10-07 07:49:58 -03:00
|
|
|
|
.. versionchanged:: 3.4
|
2012-10-28 09:59:27 -03:00
|
|
|
|
Raise :exc:`SameFileError` instead of :exc:`Error`. Since the former is
|
|
|
|
|
a subclass of the latter, this change is backward compatible.
|
2012-10-07 07:49:58 -03:00
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
Platform-specific fast-copy syscalls may be used internally in order to
|
|
|
|
|
copy the file more efficiently. See
|
|
|
|
|
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
|
2012-10-07 07:49:58 -03:00
|
|
|
|
|
|
|
|
|
.. exception:: SameFileError
|
|
|
|
|
|
|
|
|
|
This exception is raised if source and destination in :func:`copyfile`
|
|
|
|
|
are the same file.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 3.4
|
|
|
|
|
|
|
|
|
|
|
2012-07-15 20:58:29 -03:00
|
|
|
|
.. function:: copymode(src, dst, *, follow_symlinks=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
Copy the permission bits from *src* to *dst*. The file contents, owner, and
|
2019-09-09 12:51:56 -03:00
|
|
|
|
group are unaffected. *src* and *dst* are path-like objects or path names
|
|
|
|
|
given as strings.
|
2012-09-21 14:12:14 -03:00
|
|
|
|
If *follow_symlinks* is false, and both *src* and *dst* are symbolic links,
|
|
|
|
|
:func:`copymode` will attempt to modify the mode of *dst* itself (rather
|
|
|
|
|
than the file it points to). This functionality is not available on every
|
|
|
|
|
platform; please see :func:`copystat` for more information. If
|
|
|
|
|
:func:`copymode` cannot modify symbolic links on the local platform, and it
|
|
|
|
|
is asked to do so, it will do nothing and return.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.copymode src,dst shutil.copymode
|
|
|
|
|
|
2011-12-29 13:54:15 -04:00
|
|
|
|
.. versionchanged:: 3.3
|
2012-07-15 20:58:29 -03:00
|
|
|
|
Added *follow_symlinks* argument.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-07-15 20:58:29 -03:00
|
|
|
|
.. function:: copystat(src, dst, *, follow_symlinks=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-09-21 14:12:14 -03:00
|
|
|
|
Copy the permission bits, last access time, last modification time, and
|
|
|
|
|
flags from *src* to *dst*. On Linux, :func:`copystat` also copies the
|
|
|
|
|
"extended attributes" where possible. The file contents, owner, and
|
2019-09-09 12:51:56 -03:00
|
|
|
|
group are unaffected. *src* and *dst* are path-like objects or path
|
|
|
|
|
names given as strings.
|
2012-09-21 14:12:14 -03:00
|
|
|
|
|
|
|
|
|
If *follow_symlinks* is false, and *src* and *dst* both
|
|
|
|
|
refer to symbolic links, :func:`copystat` will operate on
|
|
|
|
|
the symbolic links themselves rather than the files the
|
2016-11-20 20:15:20 -04:00
|
|
|
|
symbolic links refer to—reading the information from the
|
2012-09-21 14:12:14 -03:00
|
|
|
|
*src* symbolic link, and writing the information to the
|
|
|
|
|
*dst* symbolic link.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
Not all platforms provide the ability to examine and
|
|
|
|
|
modify symbolic links. Python itself can tell you what
|
|
|
|
|
functionality is locally available.
|
|
|
|
|
|
|
|
|
|
* If ``os.chmod in os.supports_follow_symlinks`` is
|
|
|
|
|
``True``, :func:`copystat` can modify the permission
|
|
|
|
|
bits of a symbolic link.
|
|
|
|
|
|
|
|
|
|
* If ``os.utime in os.supports_follow_symlinks`` is
|
|
|
|
|
``True``, :func:`copystat` can modify the last access
|
|
|
|
|
and modification times of a symbolic link.
|
|
|
|
|
|
|
|
|
|
* If ``os.chflags in os.supports_follow_symlinks`` is
|
|
|
|
|
``True``, :func:`copystat` can modify the flags of
|
|
|
|
|
a symbolic link. (``os.chflags`` is not available on
|
|
|
|
|
all platforms.)
|
|
|
|
|
|
|
|
|
|
On platforms where some or all of this functionality
|
|
|
|
|
is unavailable, when asked to modify a symbolic link,
|
|
|
|
|
:func:`copystat` will copy everything it can.
|
|
|
|
|
:func:`copystat` never returns failure.
|
|
|
|
|
|
|
|
|
|
Please see :data:`os.supports_follow_symlinks`
|
|
|
|
|
for more information.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.copystat src,dst shutil.copystat
|
|
|
|
|
|
2011-12-29 13:54:15 -04:00
|
|
|
|
.. versionchanged:: 3.3
|
2012-07-15 20:58:29 -03:00
|
|
|
|
Added *follow_symlinks* argument and support for Linux extended attributes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-07-15 20:58:29 -03:00
|
|
|
|
.. function:: copy(src, dst, *, follow_symlinks=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-09-21 14:12:14 -03:00
|
|
|
|
Copies the file *src* to the file or directory *dst*. *src* and *dst*
|
2020-09-14 16:28:46 -03:00
|
|
|
|
should be :term:`path-like objects <path-like object>` or strings. If
|
|
|
|
|
*dst* specifies a directory, the file will be copied into *dst* using the
|
|
|
|
|
base filename from *src*. Returns the path to the newly created file.
|
2012-09-21 14:12:14 -03:00
|
|
|
|
|
|
|
|
|
If *follow_symlinks* is false, and *src* is a symbolic link,
|
|
|
|
|
*dst* will be created as a symbolic link. If *follow_symlinks*
|
|
|
|
|
is true and *src* is a symbolic link, *dst* will be a copy of
|
|
|
|
|
the file *src* refers to.
|
|
|
|
|
|
2017-03-10 22:17:21 -04:00
|
|
|
|
:func:`~shutil.copy` copies the file data and the file's permission
|
2012-09-21 14:12:14 -03:00
|
|
|
|
mode (see :func:`os.chmod`). Other metadata, like the
|
|
|
|
|
file's creation and modification times, is not preserved.
|
|
|
|
|
To preserve all file metadata from the original, use
|
|
|
|
|
:func:`~shutil.copy2` instead.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.copyfile src,dst shutil.copy
|
|
|
|
|
|
|
|
|
|
.. audit-event:: shutil.copymode src,dst shutil.copy
|
|
|
|
|
|
2011-12-29 13:54:15 -04:00
|
|
|
|
.. versionchanged:: 3.3
|
2012-07-15 20:58:29 -03:00
|
|
|
|
Added *follow_symlinks* argument.
|
2012-09-21 14:12:14 -03:00
|
|
|
|
Now returns path to the newly created file.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
Platform-specific fast-copy syscalls may be used internally in order to
|
|
|
|
|
copy the file more efficiently. See
|
|
|
|
|
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
|
|
|
|
|
|
2012-07-15 20:58:29 -03:00
|
|
|
|
.. function:: copy2(src, dst, *, follow_symlinks=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-09-21 14:12:14 -03:00
|
|
|
|
Identical to :func:`~shutil.copy` except that :func:`copy2`
|
2018-10-23 07:09:50 -03:00
|
|
|
|
also attempts to preserve file metadata.
|
2012-09-21 14:12:14 -03:00
|
|
|
|
|
|
|
|
|
When *follow_symlinks* is false, and *src* is a symbolic
|
|
|
|
|
link, :func:`copy2` attempts to copy all metadata from the
|
|
|
|
|
*src* symbolic link to the newly-created *dst* symbolic link.
|
|
|
|
|
However, this functionality is not available on all platforms.
|
|
|
|
|
On platforms where some or all of this functionality is
|
|
|
|
|
unavailable, :func:`copy2` will preserve all the metadata
|
2019-09-13 10:36:09 -03:00
|
|
|
|
it can; :func:`copy2` never raises an exception because it
|
|
|
|
|
cannot preserve file metadata.
|
2012-09-21 14:12:14 -03:00
|
|
|
|
|
|
|
|
|
:func:`copy2` uses :func:`copystat` to copy the file metadata.
|
|
|
|
|
Please see :func:`copystat` for more information
|
|
|
|
|
about platform support for modifying symbolic link metadata.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.copyfile src,dst shutil.copy2
|
|
|
|
|
|
|
|
|
|
.. audit-event:: shutil.copystat src,dst shutil.copy2
|
|
|
|
|
|
2011-12-29 13:54:15 -04:00
|
|
|
|
.. versionchanged:: 3.3
|
2012-07-15 20:58:29 -03:00
|
|
|
|
Added *follow_symlinks* argument, try to copy extended
|
|
|
|
|
file system attributes too (currently Linux only).
|
2012-09-21 14:12:14 -03:00
|
|
|
|
Now returns path to the newly created file.
|
2012-06-19 12:03:05 -03:00
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
Platform-specific fast-copy syscalls may be used internally in order to
|
|
|
|
|
copy the file more efficiently. See
|
|
|
|
|
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
|
|
|
|
|
|
Merged revisions 64623,64640,64665,64687,64689-64690,64719,64721,64735,64742,64744-64746,64756-64761,64767-64769,64771-64772,64774-64775,64788,64793,64835-64836 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64623 | benjamin.peterson | 2008-07-01 21:51:54 +0200 (Tue, 01 Jul 2008) | 1 line
write a short little section for multiprocessing; it still needs help
........
r64640 | georg.brandl | 2008-07-01 22:56:03 +0200 (Tue, 01 Jul 2008) | 2 lines
Add a comment about incref'ing w.
........
r64665 | jesse.noller | 2008-07-02 18:56:51 +0200 (Wed, 02 Jul 2008) | 1 line
Add #!/usr/bin/env python for ben
........
r64687 | andrew.kuchling | 2008-07-03 14:50:03 +0200 (Thu, 03 Jul 2008) | 1 line
Tweak wording
........
r64689 | benjamin.peterson | 2008-07-03 14:57:35 +0200 (Thu, 03 Jul 2008) | 1 line
lowercase glossary term
........
r64690 | benjamin.peterson | 2008-07-03 15:01:17 +0200 (Thu, 03 Jul 2008) | 1 line
let the term be linked
........
r64719 | raymond.hettinger | 2008-07-05 04:11:55 +0200 (Sat, 05 Jul 2008) | 1 line
Update comment on prediction macros.
........
r64721 | georg.brandl | 2008-07-05 12:07:18 +0200 (Sat, 05 Jul 2008) | 2 lines
Fix tabs.
........
r64735 | mark.dickinson | 2008-07-05 17:25:48 +0200 (Sat, 05 Jul 2008) | 3 lines
Minor rewrite of cmath_log to work around a Sun compiler bug. See issue
#3168.
........
r64742 | benjamin.peterson | 2008-07-05 18:29:38 +0200 (Sat, 05 Jul 2008) | 1 line
make regrtest aware of the lib2to3 resource
........
r64744 | georg.brandl | 2008-07-05 18:43:45 +0200 (Sat, 05 Jul 2008) | 2 lines
Keep below 80 chars.
........
r64745 | facundo.batista | 2008-07-05 21:19:50 +0200 (Sat, 05 Jul 2008) | 3 lines
Issue 3289. Removed two lines that ended doing nothing.
........
r64746 | facundo.batista | 2008-07-05 22:39:59 +0200 (Sat, 05 Jul 2008) | 4 lines
Issue #3239. Differentiate the ascii call from the curses one and
the builtin one.
........
r64756 | gregory.p.smith | 2008-07-06 09:16:40 +0200 (Sun, 06 Jul 2008) | 3 lines
- Issue #2113: Fix error in subprocess.Popen if the select system call is
interrupted by a signal.
........
r64757 | benjamin.peterson | 2008-07-06 14:39:09 +0200 (Sun, 06 Jul 2008) | 1 line
remove test_compact_freelists from test_sys
........
r64758 | gregory.p.smith | 2008-07-06 19:06:29 +0200 (Sun, 06 Jul 2008) | 2 lines
fix issue3304 - remove an incorrect PyMem_Free in fileio_init
........
r64759 | georg.brandl | 2008-07-06 19:36:20 +0200 (Sun, 06 Jul 2008) | 2 lines
Fix opensearch template.
........
r64760 | andrew.kuchling | 2008-07-06 19:43:16 +0200 (Sun, 06 Jul 2008) | 1 line
Wording fix
........
r64761 | andrew.kuchling | 2008-07-06 19:44:17 +0200 (Sun, 06 Jul 2008) | 1 line
Add two items; rewrap paragraph
........
r64767 | gregory.p.smith | 2008-07-07 06:31:58 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3309: Fix bz2.BZFile itererator to release its internal lock
properly when raising an exception due to the bz2file being closed.
Prevents a deadlock.
........
r64768 | josiah.carlson | 2008-07-07 06:51:46 +0200 (Mon, 07 Jul 2008) | 2 lines
Fixed bugs 760475, 953599, and 1519.
........
r64769 | gregory.p.smith | 2008-07-07 06:54:31 +0200 (Mon, 07 Jul 2008) | 2 lines
Add commented out #_sha256 and #_sha512 lines per issue 3183.
........
r64771 | gregory.p.smith | 2008-07-07 07:09:12 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3094: httplib.HTTPSConnection Host: headers no longer include the
redundant ":443" port number designation when the connection is using the
default https port (443).
........
r64772 | skip.montanaro | 2008-07-07 13:16:14 +0200 (Mon, 07 Jul 2008) | 2 lines
Correct grammar.
........
r64774 | andrew.kuchling | 2008-07-07 18:51:09 +0200 (Mon, 07 Jul 2008) | 1 line
Fix example to match text
........
r64775 | facundo.batista | 2008-07-07 19:02:59 +0200 (Mon, 07 Jul 2008) | 3 lines
Issue 3306. Better control for a lenght in findmax() function.
........
r64788 | georg.brandl | 2008-07-08 09:05:23 +0200 (Tue, 08 Jul 2008) | 2 lines
Add missing ABCs to list.
........
r64793 | nick.coghlan | 2008-07-08 16:21:42 +0200 (Tue, 08 Jul 2008) | 1 line
Add missing NEWS and ACK entries for r64791
........
r64835 | raymond.hettinger | 2008-07-10 11:31:08 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3287: Raise correct exception for float inputs.
........
r64836 | raymond.hettinger | 2008-07-10 12:28:41 +0200 (Thu, 10 Jul 2008) | 1 line
Use operator.index() instead of n.__index__().
........
2008-07-16 00:43:04 -03:00
|
|
|
|
.. function:: ignore_patterns(\*patterns)
|
|
|
|
|
|
|
|
|
|
This factory function creates a function that can be used as a callable for
|
|
|
|
|
:func:`copytree`\'s *ignore* argument, ignoring files and directories that
|
|
|
|
|
match one of the glob-style *patterns* provided. See the example below.
|
|
|
|
|
|
|
|
|
|
|
2014-06-11 15:40:13 -03:00
|
|
|
|
.. function:: copytree(src, dst, symlinks=False, ignore=None, \
|
2018-12-28 14:03:40 -04:00
|
|
|
|
copy_function=copy2, ignore_dangling_symlinks=False, \
|
|
|
|
|
dirs_exist_ok=False)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2018-12-28 14:03:40 -04:00
|
|
|
|
Recursively copy an entire directory tree rooted at *src* to a directory
|
|
|
|
|
named *dst* and return the destination directory. *dirs_exist_ok* dictates
|
|
|
|
|
whether to raise an exception in case *dst* or any missing parent directory
|
|
|
|
|
already exists.
|
|
|
|
|
|
|
|
|
|
Permissions and times of directories are copied with :func:`copystat`,
|
2019-05-10 23:51:45 -03:00
|
|
|
|
individual files are copied using :func:`~shutil.copy2`.
|
Merged revisions 64623,64640,64665,64687,64689-64690,64719,64721,64735,64742,64744-64746,64756-64761,64767-64769,64771-64772,64774-64775,64788,64793,64835-64836 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64623 | benjamin.peterson | 2008-07-01 21:51:54 +0200 (Tue, 01 Jul 2008) | 1 line
write a short little section for multiprocessing; it still needs help
........
r64640 | georg.brandl | 2008-07-01 22:56:03 +0200 (Tue, 01 Jul 2008) | 2 lines
Add a comment about incref'ing w.
........
r64665 | jesse.noller | 2008-07-02 18:56:51 +0200 (Wed, 02 Jul 2008) | 1 line
Add #!/usr/bin/env python for ben
........
r64687 | andrew.kuchling | 2008-07-03 14:50:03 +0200 (Thu, 03 Jul 2008) | 1 line
Tweak wording
........
r64689 | benjamin.peterson | 2008-07-03 14:57:35 +0200 (Thu, 03 Jul 2008) | 1 line
lowercase glossary term
........
r64690 | benjamin.peterson | 2008-07-03 15:01:17 +0200 (Thu, 03 Jul 2008) | 1 line
let the term be linked
........
r64719 | raymond.hettinger | 2008-07-05 04:11:55 +0200 (Sat, 05 Jul 2008) | 1 line
Update comment on prediction macros.
........
r64721 | georg.brandl | 2008-07-05 12:07:18 +0200 (Sat, 05 Jul 2008) | 2 lines
Fix tabs.
........
r64735 | mark.dickinson | 2008-07-05 17:25:48 +0200 (Sat, 05 Jul 2008) | 3 lines
Minor rewrite of cmath_log to work around a Sun compiler bug. See issue
#3168.
........
r64742 | benjamin.peterson | 2008-07-05 18:29:38 +0200 (Sat, 05 Jul 2008) | 1 line
make regrtest aware of the lib2to3 resource
........
r64744 | georg.brandl | 2008-07-05 18:43:45 +0200 (Sat, 05 Jul 2008) | 2 lines
Keep below 80 chars.
........
r64745 | facundo.batista | 2008-07-05 21:19:50 +0200 (Sat, 05 Jul 2008) | 3 lines
Issue 3289. Removed two lines that ended doing nothing.
........
r64746 | facundo.batista | 2008-07-05 22:39:59 +0200 (Sat, 05 Jul 2008) | 4 lines
Issue #3239. Differentiate the ascii call from the curses one and
the builtin one.
........
r64756 | gregory.p.smith | 2008-07-06 09:16:40 +0200 (Sun, 06 Jul 2008) | 3 lines
- Issue #2113: Fix error in subprocess.Popen if the select system call is
interrupted by a signal.
........
r64757 | benjamin.peterson | 2008-07-06 14:39:09 +0200 (Sun, 06 Jul 2008) | 1 line
remove test_compact_freelists from test_sys
........
r64758 | gregory.p.smith | 2008-07-06 19:06:29 +0200 (Sun, 06 Jul 2008) | 2 lines
fix issue3304 - remove an incorrect PyMem_Free in fileio_init
........
r64759 | georg.brandl | 2008-07-06 19:36:20 +0200 (Sun, 06 Jul 2008) | 2 lines
Fix opensearch template.
........
r64760 | andrew.kuchling | 2008-07-06 19:43:16 +0200 (Sun, 06 Jul 2008) | 1 line
Wording fix
........
r64761 | andrew.kuchling | 2008-07-06 19:44:17 +0200 (Sun, 06 Jul 2008) | 1 line
Add two items; rewrap paragraph
........
r64767 | gregory.p.smith | 2008-07-07 06:31:58 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3309: Fix bz2.BZFile itererator to release its internal lock
properly when raising an exception due to the bz2file being closed.
Prevents a deadlock.
........
r64768 | josiah.carlson | 2008-07-07 06:51:46 +0200 (Mon, 07 Jul 2008) | 2 lines
Fixed bugs 760475, 953599, and 1519.
........
r64769 | gregory.p.smith | 2008-07-07 06:54:31 +0200 (Mon, 07 Jul 2008) | 2 lines
Add commented out #_sha256 and #_sha512 lines per issue 3183.
........
r64771 | gregory.p.smith | 2008-07-07 07:09:12 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3094: httplib.HTTPSConnection Host: headers no longer include the
redundant ":443" port number designation when the connection is using the
default https port (443).
........
r64772 | skip.montanaro | 2008-07-07 13:16:14 +0200 (Mon, 07 Jul 2008) | 2 lines
Correct grammar.
........
r64774 | andrew.kuchling | 2008-07-07 18:51:09 +0200 (Mon, 07 Jul 2008) | 1 line
Fix example to match text
........
r64775 | facundo.batista | 2008-07-07 19:02:59 +0200 (Mon, 07 Jul 2008) | 3 lines
Issue 3306. Better control for a lenght in findmax() function.
........
r64788 | georg.brandl | 2008-07-08 09:05:23 +0200 (Tue, 08 Jul 2008) | 2 lines
Add missing ABCs to list.
........
r64793 | nick.coghlan | 2008-07-08 16:21:42 +0200 (Tue, 08 Jul 2008) | 1 line
Add missing NEWS and ACK entries for r64791
........
r64835 | raymond.hettinger | 2008-07-10 11:31:08 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3287: Raise correct exception for float inputs.
........
r64836 | raymond.hettinger | 2008-07-10 12:28:41 +0200 (Thu, 10 Jul 2008) | 1 line
Use operator.index() instead of n.__index__().
........
2008-07-16 00:43:04 -03:00
|
|
|
|
|
|
|
|
|
If *symlinks* is true, symbolic links in the source tree are represented as
|
2011-12-29 13:54:15 -04:00
|
|
|
|
symbolic links in the new tree and the metadata of the original links will
|
|
|
|
|
be copied as far as the platform allows; if false or omitted, the contents
|
|
|
|
|
and metadata of the linked files are copied to the new tree.
|
Merged revisions 64623,64640,64665,64687,64689-64690,64719,64721,64735,64742,64744-64746,64756-64761,64767-64769,64771-64772,64774-64775,64788,64793,64835-64836 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64623 | benjamin.peterson | 2008-07-01 21:51:54 +0200 (Tue, 01 Jul 2008) | 1 line
write a short little section for multiprocessing; it still needs help
........
r64640 | georg.brandl | 2008-07-01 22:56:03 +0200 (Tue, 01 Jul 2008) | 2 lines
Add a comment about incref'ing w.
........
r64665 | jesse.noller | 2008-07-02 18:56:51 +0200 (Wed, 02 Jul 2008) | 1 line
Add #!/usr/bin/env python for ben
........
r64687 | andrew.kuchling | 2008-07-03 14:50:03 +0200 (Thu, 03 Jul 2008) | 1 line
Tweak wording
........
r64689 | benjamin.peterson | 2008-07-03 14:57:35 +0200 (Thu, 03 Jul 2008) | 1 line
lowercase glossary term
........
r64690 | benjamin.peterson | 2008-07-03 15:01:17 +0200 (Thu, 03 Jul 2008) | 1 line
let the term be linked
........
r64719 | raymond.hettinger | 2008-07-05 04:11:55 +0200 (Sat, 05 Jul 2008) | 1 line
Update comment on prediction macros.
........
r64721 | georg.brandl | 2008-07-05 12:07:18 +0200 (Sat, 05 Jul 2008) | 2 lines
Fix tabs.
........
r64735 | mark.dickinson | 2008-07-05 17:25:48 +0200 (Sat, 05 Jul 2008) | 3 lines
Minor rewrite of cmath_log to work around a Sun compiler bug. See issue
#3168.
........
r64742 | benjamin.peterson | 2008-07-05 18:29:38 +0200 (Sat, 05 Jul 2008) | 1 line
make regrtest aware of the lib2to3 resource
........
r64744 | georg.brandl | 2008-07-05 18:43:45 +0200 (Sat, 05 Jul 2008) | 2 lines
Keep below 80 chars.
........
r64745 | facundo.batista | 2008-07-05 21:19:50 +0200 (Sat, 05 Jul 2008) | 3 lines
Issue 3289. Removed two lines that ended doing nothing.
........
r64746 | facundo.batista | 2008-07-05 22:39:59 +0200 (Sat, 05 Jul 2008) | 4 lines
Issue #3239. Differentiate the ascii call from the curses one and
the builtin one.
........
r64756 | gregory.p.smith | 2008-07-06 09:16:40 +0200 (Sun, 06 Jul 2008) | 3 lines
- Issue #2113: Fix error in subprocess.Popen if the select system call is
interrupted by a signal.
........
r64757 | benjamin.peterson | 2008-07-06 14:39:09 +0200 (Sun, 06 Jul 2008) | 1 line
remove test_compact_freelists from test_sys
........
r64758 | gregory.p.smith | 2008-07-06 19:06:29 +0200 (Sun, 06 Jul 2008) | 2 lines
fix issue3304 - remove an incorrect PyMem_Free in fileio_init
........
r64759 | georg.brandl | 2008-07-06 19:36:20 +0200 (Sun, 06 Jul 2008) | 2 lines
Fix opensearch template.
........
r64760 | andrew.kuchling | 2008-07-06 19:43:16 +0200 (Sun, 06 Jul 2008) | 1 line
Wording fix
........
r64761 | andrew.kuchling | 2008-07-06 19:44:17 +0200 (Sun, 06 Jul 2008) | 1 line
Add two items; rewrap paragraph
........
r64767 | gregory.p.smith | 2008-07-07 06:31:58 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3309: Fix bz2.BZFile itererator to release its internal lock
properly when raising an exception due to the bz2file being closed.
Prevents a deadlock.
........
r64768 | josiah.carlson | 2008-07-07 06:51:46 +0200 (Mon, 07 Jul 2008) | 2 lines
Fixed bugs 760475, 953599, and 1519.
........
r64769 | gregory.p.smith | 2008-07-07 06:54:31 +0200 (Mon, 07 Jul 2008) | 2 lines
Add commented out #_sha256 and #_sha512 lines per issue 3183.
........
r64771 | gregory.p.smith | 2008-07-07 07:09:12 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3094: httplib.HTTPSConnection Host: headers no longer include the
redundant ":443" port number designation when the connection is using the
default https port (443).
........
r64772 | skip.montanaro | 2008-07-07 13:16:14 +0200 (Mon, 07 Jul 2008) | 2 lines
Correct grammar.
........
r64774 | andrew.kuchling | 2008-07-07 18:51:09 +0200 (Mon, 07 Jul 2008) | 1 line
Fix example to match text
........
r64775 | facundo.batista | 2008-07-07 19:02:59 +0200 (Mon, 07 Jul 2008) | 3 lines
Issue 3306. Better control for a lenght in findmax() function.
........
r64788 | georg.brandl | 2008-07-08 09:05:23 +0200 (Tue, 08 Jul 2008) | 2 lines
Add missing ABCs to list.
........
r64793 | nick.coghlan | 2008-07-08 16:21:42 +0200 (Tue, 08 Jul 2008) | 1 line
Add missing NEWS and ACK entries for r64791
........
r64835 | raymond.hettinger | 2008-07-10 11:31:08 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3287: Raise correct exception for float inputs.
........
r64836 | raymond.hettinger | 2008-07-10 12:28:41 +0200 (Thu, 10 Jul 2008) | 1 line
Use operator.index() instead of n.__index__().
........
2008-07-16 00:43:04 -03:00
|
|
|
|
|
2010-04-20 05:57:33 -03:00
|
|
|
|
When *symlinks* is false, if the file pointed by the symlink doesn't
|
2015-11-01 23:37:02 -04:00
|
|
|
|
exist, an exception will be added in the list of errors raised in
|
|
|
|
|
an :exc:`Error` exception at the end of the copy process.
|
2010-04-20 05:57:33 -03:00
|
|
|
|
You can set the optional *ignore_dangling_symlinks* flag to true if you
|
2010-04-23 10:03:50 -03:00
|
|
|
|
want to silence this exception. Notice that this option has no effect
|
|
|
|
|
on platforms that don't support :func:`os.symlink`.
|
2010-04-20 05:57:33 -03:00
|
|
|
|
|
Merged revisions 64623,64640,64665,64687,64689-64690,64719,64721,64735,64742,64744-64746,64756-64761,64767-64769,64771-64772,64774-64775,64788,64793,64835-64836 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64623 | benjamin.peterson | 2008-07-01 21:51:54 +0200 (Tue, 01 Jul 2008) | 1 line
write a short little section for multiprocessing; it still needs help
........
r64640 | georg.brandl | 2008-07-01 22:56:03 +0200 (Tue, 01 Jul 2008) | 2 lines
Add a comment about incref'ing w.
........
r64665 | jesse.noller | 2008-07-02 18:56:51 +0200 (Wed, 02 Jul 2008) | 1 line
Add #!/usr/bin/env python for ben
........
r64687 | andrew.kuchling | 2008-07-03 14:50:03 +0200 (Thu, 03 Jul 2008) | 1 line
Tweak wording
........
r64689 | benjamin.peterson | 2008-07-03 14:57:35 +0200 (Thu, 03 Jul 2008) | 1 line
lowercase glossary term
........
r64690 | benjamin.peterson | 2008-07-03 15:01:17 +0200 (Thu, 03 Jul 2008) | 1 line
let the term be linked
........
r64719 | raymond.hettinger | 2008-07-05 04:11:55 +0200 (Sat, 05 Jul 2008) | 1 line
Update comment on prediction macros.
........
r64721 | georg.brandl | 2008-07-05 12:07:18 +0200 (Sat, 05 Jul 2008) | 2 lines
Fix tabs.
........
r64735 | mark.dickinson | 2008-07-05 17:25:48 +0200 (Sat, 05 Jul 2008) | 3 lines
Minor rewrite of cmath_log to work around a Sun compiler bug. See issue
#3168.
........
r64742 | benjamin.peterson | 2008-07-05 18:29:38 +0200 (Sat, 05 Jul 2008) | 1 line
make regrtest aware of the lib2to3 resource
........
r64744 | georg.brandl | 2008-07-05 18:43:45 +0200 (Sat, 05 Jul 2008) | 2 lines
Keep below 80 chars.
........
r64745 | facundo.batista | 2008-07-05 21:19:50 +0200 (Sat, 05 Jul 2008) | 3 lines
Issue 3289. Removed two lines that ended doing nothing.
........
r64746 | facundo.batista | 2008-07-05 22:39:59 +0200 (Sat, 05 Jul 2008) | 4 lines
Issue #3239. Differentiate the ascii call from the curses one and
the builtin one.
........
r64756 | gregory.p.smith | 2008-07-06 09:16:40 +0200 (Sun, 06 Jul 2008) | 3 lines
- Issue #2113: Fix error in subprocess.Popen if the select system call is
interrupted by a signal.
........
r64757 | benjamin.peterson | 2008-07-06 14:39:09 +0200 (Sun, 06 Jul 2008) | 1 line
remove test_compact_freelists from test_sys
........
r64758 | gregory.p.smith | 2008-07-06 19:06:29 +0200 (Sun, 06 Jul 2008) | 2 lines
fix issue3304 - remove an incorrect PyMem_Free in fileio_init
........
r64759 | georg.brandl | 2008-07-06 19:36:20 +0200 (Sun, 06 Jul 2008) | 2 lines
Fix opensearch template.
........
r64760 | andrew.kuchling | 2008-07-06 19:43:16 +0200 (Sun, 06 Jul 2008) | 1 line
Wording fix
........
r64761 | andrew.kuchling | 2008-07-06 19:44:17 +0200 (Sun, 06 Jul 2008) | 1 line
Add two items; rewrap paragraph
........
r64767 | gregory.p.smith | 2008-07-07 06:31:58 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3309: Fix bz2.BZFile itererator to release its internal lock
properly when raising an exception due to the bz2file being closed.
Prevents a deadlock.
........
r64768 | josiah.carlson | 2008-07-07 06:51:46 +0200 (Mon, 07 Jul 2008) | 2 lines
Fixed bugs 760475, 953599, and 1519.
........
r64769 | gregory.p.smith | 2008-07-07 06:54:31 +0200 (Mon, 07 Jul 2008) | 2 lines
Add commented out #_sha256 and #_sha512 lines per issue 3183.
........
r64771 | gregory.p.smith | 2008-07-07 07:09:12 +0200 (Mon, 07 Jul 2008) | 4 lines
- Issue #3094: httplib.HTTPSConnection Host: headers no longer include the
redundant ":443" port number designation when the connection is using the
default https port (443).
........
r64772 | skip.montanaro | 2008-07-07 13:16:14 +0200 (Mon, 07 Jul 2008) | 2 lines
Correct grammar.
........
r64774 | andrew.kuchling | 2008-07-07 18:51:09 +0200 (Mon, 07 Jul 2008) | 1 line
Fix example to match text
........
r64775 | facundo.batista | 2008-07-07 19:02:59 +0200 (Mon, 07 Jul 2008) | 3 lines
Issue 3306. Better control for a lenght in findmax() function.
........
r64788 | georg.brandl | 2008-07-08 09:05:23 +0200 (Tue, 08 Jul 2008) | 2 lines
Add missing ABCs to list.
........
r64793 | nick.coghlan | 2008-07-08 16:21:42 +0200 (Tue, 08 Jul 2008) | 1 line
Add missing NEWS and ACK entries for r64791
........
r64835 | raymond.hettinger | 2008-07-10 11:31:08 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3287: Raise correct exception for float inputs.
........
r64836 | raymond.hettinger | 2008-07-10 12:28:41 +0200 (Thu, 10 Jul 2008) | 1 line
Use operator.index() instead of n.__index__().
........
2008-07-16 00:43:04 -03:00
|
|
|
|
If *ignore* is given, it must be a callable that will receive as its
|
|
|
|
|
arguments the directory being visited by :func:`copytree`, and a list of its
|
|
|
|
|
contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
|
|
|
|
|
called recursively, the *ignore* callable will be called once for each
|
|
|
|
|
directory that is copied. The callable must return a sequence of directory
|
|
|
|
|
and file names relative to the current directory (i.e. a subset of the items
|
|
|
|
|
in its second argument); these names will then be ignored in the copy
|
|
|
|
|
process. :func:`ignore_patterns` can be used to create such a callable that
|
|
|
|
|
ignores names based on glob-style patterns.
|
|
|
|
|
|
|
|
|
|
If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
|
|
|
|
|
|
2012-02-13 11:30:47 -04:00
|
|
|
|
If *copy_function* is given, it must be a callable that will be used to copy
|
|
|
|
|
each file. It will be called with the source path and the destination path
|
2019-05-10 23:51:45 -03:00
|
|
|
|
as arguments. By default, :func:`~shutil.copy2` is used, but any function
|
|
|
|
|
that supports the same signature (like :func:`~shutil.copy`) can be used.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
|
.. audit-event:: shutil.copytree src,dst shutil.copytree
|
2019-06-24 12:42:54 -03:00
|
|
|
|
|
2012-07-15 20:58:29 -03:00
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
|
Copy metadata when *symlinks* is false.
|
|
|
|
|
Now returns *dst*.
|
|
|
|
|
|
2010-04-19 19:30:51 -03:00
|
|
|
|
.. versionchanged:: 3.2
|
|
|
|
|
Added the *copy_function* argument to be able to provide a custom copy
|
|
|
|
|
function.
|
2010-04-20 05:57:33 -03:00
|
|
|
|
Added the *ignore_dangling_symlinks* argument to silent dangling symlinks
|
|
|
|
|
errors when *symlinks* is false.
|
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
Platform-specific fast-copy syscalls may be used internally in order to
|
|
|
|
|
copy the file more efficiently. See
|
|
|
|
|
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
|
2012-06-24 12:39:05 -03:00
|
|
|
|
|
2018-12-28 14:03:40 -04:00
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
|
The *dirs_exist_ok* parameter.
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
|
.. function:: rmtree(path, ignore_errors=False, onerror=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. index:: single: directory; deleting
|
|
|
|
|
|
Merged revisions 60124-60142 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60131 | georg.brandl | 2008-01-20 12:13:29 +0100 (Sun, 20 Jan 2008) | 3 lines
#1351692: in pprint, always call format() for dict and list items to enable
custom formatting of contents via subclassing PrettyPrinter.
........
r60133 | georg.brandl | 2008-01-20 12:43:03 +0100 (Sun, 20 Jan 2008) | 2 lines
#1178141: add addinfourl.code to get http status code from urllib.
........
r60134 | georg.brandl | 2008-01-20 13:05:43 +0100 (Sun, 20 Jan 2008) | 4 lines
#856047: respect the ``no_proxy`` env var when checking for proxies
in urllib and using the other ``_proxy`` env vars.
Original patch by Donovan Baarda.
........
r60135 | georg.brandl | 2008-01-20 13:18:17 +0100 (Sun, 20 Jan 2008) | 4 lines
#1664522: in urllib, don't read non-existing directories in ftp mode,
returning a 0-byte file -- raise an IOError instead.
Original patch from Phil Knirsch.
........
r60136 | georg.brandl | 2008-01-20 13:57:47 +0100 (Sun, 20 Jan 2008) | 2 lines
#799369: document possible sys.platform values.
........
r60137 | georg.brandl | 2008-01-20 14:08:37 +0100 (Sun, 20 Jan 2008) | 2 lines
#652749: document the constants added to the builtins by site.py.
........
r60138 | georg.brandl | 2008-01-20 14:59:46 +0100 (Sun, 20 Jan 2008) | 2 lines
#1648: add sys.gettrace() and sys.getprofile().
........
r60139 | georg.brandl | 2008-01-20 15:17:42 +0100 (Sun, 20 Jan 2008) | 2 lines
#1669: don't allow shutil.rmtree() to be called on a symlink.
........
r60140 | georg.brandl | 2008-01-20 15:20:02 +0100 (Sun, 20 Jan 2008) | 2 lines
Fix test_pyclbr after urllib change.
........
r60141 | christian.heimes | 2008-01-20 15:28:28 +0100 (Sun, 20 Jan 2008) | 1 line
Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
........
r60142 | georg.brandl | 2008-01-20 15:31:27 +0100 (Sun, 20 Jan 2008) | 2 lines
#1876: fix typos in test_operator.
........
2008-01-20 11:14:11 -04:00
|
|
|
|
Delete an entire directory tree; *path* must point to a directory (but not a
|
|
|
|
|
symbolic link to a directory). If *ignore_errors* is true, errors resulting
|
|
|
|
|
from failed removals will be ignored; if false or omitted, such errors are
|
|
|
|
|
handled by calling a handler specified by *onerror* or, if that is omitted,
|
|
|
|
|
they raise an exception.
|
|
|
|
|
|
2012-06-24 03:43:06 -03:00
|
|
|
|
.. note::
|
2012-06-23 12:58:42 -03:00
|
|
|
|
|
2012-06-24 03:43:06 -03:00
|
|
|
|
On platforms that support the necessary fd-based functions a symlink
|
2012-06-24 12:39:05 -03:00
|
|
|
|
attack resistant version of :func:`rmtree` is used by default. On other
|
|
|
|
|
platforms, the :func:`rmtree` implementation is susceptible to a symlink
|
|
|
|
|
attack: given proper timing and circumstances, attackers can manipulate
|
|
|
|
|
symlinks on the filesystem to delete files they wouldn't be able to access
|
|
|
|
|
otherwise. Applications can use the :data:`rmtree.avoids_symlink_attacks`
|
|
|
|
|
function attribute to determine which case applies.
|
2012-06-23 12:58:42 -03:00
|
|
|
|
|
Merged revisions 60124-60142 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60131 | georg.brandl | 2008-01-20 12:13:29 +0100 (Sun, 20 Jan 2008) | 3 lines
#1351692: in pprint, always call format() for dict and list items to enable
custom formatting of contents via subclassing PrettyPrinter.
........
r60133 | georg.brandl | 2008-01-20 12:43:03 +0100 (Sun, 20 Jan 2008) | 2 lines
#1178141: add addinfourl.code to get http status code from urllib.
........
r60134 | georg.brandl | 2008-01-20 13:05:43 +0100 (Sun, 20 Jan 2008) | 4 lines
#856047: respect the ``no_proxy`` env var when checking for proxies
in urllib and using the other ``_proxy`` env vars.
Original patch by Donovan Baarda.
........
r60135 | georg.brandl | 2008-01-20 13:18:17 +0100 (Sun, 20 Jan 2008) | 4 lines
#1664522: in urllib, don't read non-existing directories in ftp mode,
returning a 0-byte file -- raise an IOError instead.
Original patch from Phil Knirsch.
........
r60136 | georg.brandl | 2008-01-20 13:57:47 +0100 (Sun, 20 Jan 2008) | 2 lines
#799369: document possible sys.platform values.
........
r60137 | georg.brandl | 2008-01-20 14:08:37 +0100 (Sun, 20 Jan 2008) | 2 lines
#652749: document the constants added to the builtins by site.py.
........
r60138 | georg.brandl | 2008-01-20 14:59:46 +0100 (Sun, 20 Jan 2008) | 2 lines
#1648: add sys.gettrace() and sys.getprofile().
........
r60139 | georg.brandl | 2008-01-20 15:17:42 +0100 (Sun, 20 Jan 2008) | 2 lines
#1669: don't allow shutil.rmtree() to be called on a symlink.
........
r60140 | georg.brandl | 2008-01-20 15:20:02 +0100 (Sun, 20 Jan 2008) | 2 lines
Fix test_pyclbr after urllib change.
........
r60141 | christian.heimes | 2008-01-20 15:28:28 +0100 (Sun, 20 Jan 2008) | 1 line
Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
........
r60142 | georg.brandl | 2008-01-20 15:31:27 +0100 (Sun, 20 Jan 2008) | 2 lines
#1876: fix typos in test_operator.
........
2008-01-20 11:14:11 -04:00
|
|
|
|
If *onerror* is provided, it must be a callable that accepts three
|
2012-06-23 12:58:42 -03:00
|
|
|
|
parameters: *function*, *path*, and *excinfo*.
|
|
|
|
|
|
|
|
|
|
The first parameter, *function*, is the function which raised the exception;
|
|
|
|
|
it depends on the platform and implementation. The second parameter,
|
|
|
|
|
*path*, will be the path name passed to *function*. The third parameter,
|
|
|
|
|
*excinfo*, will be the exception information returned by
|
|
|
|
|
:func:`sys.exc_info`. Exceptions raised by *onerror* will not be caught.
|
|
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
|
.. audit-event:: shutil.rmtree path shutil.rmtree
|
2019-06-24 12:42:54 -03:00
|
|
|
|
|
2012-06-23 12:58:42 -03:00
|
|
|
|
.. versionchanged:: 3.3
|
2012-06-24 03:43:06 -03:00
|
|
|
|
Added a symlink attack resistant version that is used automatically
|
|
|
|
|
if platform supports fd-based functions.
|
Merged revisions 60124-60142 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60131 | georg.brandl | 2008-01-20 12:13:29 +0100 (Sun, 20 Jan 2008) | 3 lines
#1351692: in pprint, always call format() for dict and list items to enable
custom formatting of contents via subclassing PrettyPrinter.
........
r60133 | georg.brandl | 2008-01-20 12:43:03 +0100 (Sun, 20 Jan 2008) | 2 lines
#1178141: add addinfourl.code to get http status code from urllib.
........
r60134 | georg.brandl | 2008-01-20 13:05:43 +0100 (Sun, 20 Jan 2008) | 4 lines
#856047: respect the ``no_proxy`` env var when checking for proxies
in urllib and using the other ``_proxy`` env vars.
Original patch by Donovan Baarda.
........
r60135 | georg.brandl | 2008-01-20 13:18:17 +0100 (Sun, 20 Jan 2008) | 4 lines
#1664522: in urllib, don't read non-existing directories in ftp mode,
returning a 0-byte file -- raise an IOError instead.
Original patch from Phil Knirsch.
........
r60136 | georg.brandl | 2008-01-20 13:57:47 +0100 (Sun, 20 Jan 2008) | 2 lines
#799369: document possible sys.platform values.
........
r60137 | georg.brandl | 2008-01-20 14:08:37 +0100 (Sun, 20 Jan 2008) | 2 lines
#652749: document the constants added to the builtins by site.py.
........
r60138 | georg.brandl | 2008-01-20 14:59:46 +0100 (Sun, 20 Jan 2008) | 2 lines
#1648: add sys.gettrace() and sys.getprofile().
........
r60139 | georg.brandl | 2008-01-20 15:17:42 +0100 (Sun, 20 Jan 2008) | 2 lines
#1669: don't allow shutil.rmtree() to be called on a symlink.
........
r60140 | georg.brandl | 2008-01-20 15:20:02 +0100 (Sun, 20 Jan 2008) | 2 lines
Fix test_pyclbr after urllib change.
........
r60141 | christian.heimes | 2008-01-20 15:28:28 +0100 (Sun, 20 Jan 2008) | 1 line
Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
........
r60142 | georg.brandl | 2008-01-20 15:31:27 +0100 (Sun, 20 Jan 2008) | 2 lines
#1876: fix typos in test_operator.
........
2008-01-20 11:14:11 -04:00
|
|
|
|
|
2019-08-21 19:27:33 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
On Windows, will no longer delete the contents of a directory junction
|
|
|
|
|
before removing the junction.
|
|
|
|
|
|
2012-06-24 14:53:48 -03:00
|
|
|
|
.. attribute:: rmtree.avoids_symlink_attacks
|
2012-06-23 15:28:32 -03:00
|
|
|
|
|
2012-06-24 03:43:06 -03:00
|
|
|
|
Indicates whether the current platform and implementation provides a
|
2012-06-24 12:39:05 -03:00
|
|
|
|
symlink attack resistant version of :func:`rmtree`. Currently this is
|
2012-06-24 03:43:06 -03:00
|
|
|
|
only true for platforms supporting fd-based directory access functions.
|
2012-06-23 15:28:32 -03:00
|
|
|
|
|
2012-06-24 03:43:06 -03:00
|
|
|
|
.. versionadded:: 3.3
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-06-24 12:39:05 -03:00
|
|
|
|
|
2014-06-11 15:40:13 -03:00
|
|
|
|
.. function:: move(src, dst, copy_function=copy2)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-06-18 20:41:07 -03:00
|
|
|
|
Recursively move a file or directory (*src*) to another location (*dst*)
|
|
|
|
|
and return the destination.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2015-03-22 11:11:54 -03:00
|
|
|
|
If the destination is an existing directory, then *src* is moved inside that
|
|
|
|
|
directory. If the destination already exists but is not a directory, it may
|
|
|
|
|
be overwritten depending on :func:`os.rename` semantics.
|
2011-07-28 17:49:11 -03:00
|
|
|
|
|
|
|
|
|
If the destination is on the current filesystem, then :func:`os.rename` is
|
2014-06-11 15:40:13 -03:00
|
|
|
|
used. Otherwise, *src* is copied to *dst* using *copy_function* and then
|
|
|
|
|
removed. In case of symlinks, a new symlink pointing to the target of *src*
|
|
|
|
|
will be created in or as *dst* and *src* will be removed.
|
|
|
|
|
|
|
|
|
|
If *copy_function* is given, it must be a callable that takes two arguments
|
|
|
|
|
*src* and *dst*, and will be used to copy *src* to *dest* if
|
|
|
|
|
:func:`os.rename` cannot be used. If the source is a directory,
|
|
|
|
|
:func:`copytree` is called, passing it the :func:`copy_function`. The
|
2017-03-10 22:17:21 -04:00
|
|
|
|
default *copy_function* is :func:`copy2`. Using :func:`~shutil.copy` as the
|
2014-06-11 15:40:13 -03:00
|
|
|
|
*copy_function* allows the move to succeed when it is not possible to also
|
|
|
|
|
copy the metadata, at the expense of not copying any of the metadata.
|
2012-01-06 15:16:19 -04:00
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.move src,dst shutil.move
|
|
|
|
|
|
2012-01-06 15:16:19 -04:00
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
|
Added explicit symlink handling for foreign filesystems, thus adapting
|
|
|
|
|
it to the behavior of GNU's :program:`mv`.
|
2012-07-15 20:58:29 -03:00
|
|
|
|
Now returns *dst*.
|
2012-06-19 12:03:05 -03:00
|
|
|
|
|
2014-06-11 15:40:13 -03:00
|
|
|
|
.. versionchanged:: 3.5
|
|
|
|
|
Added the *copy_function* keyword argument.
|
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
Platform-specific fast-copy syscalls may be used internally in order to
|
|
|
|
|
copy the file more efficiently. See
|
|
|
|
|
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
|
|
|
|
|
|
bpo-32689: Updates shutil.move to allow for Path objects to be used as source arg (GH-15326)
Important work originally done by @emilyemorehouse two years ago and nearly ready to go in.
This bug has affected many people and in some cases has been a dealbreaker to the adoption of the otherwise wonderful pathlib and PEP519. https://stackoverflow.com/questions/33625931/copy-file-with-pathlib-in-python.
This adds the outstanding test request from that PR @vstinner (https://github.com/python/cpython/pull/5393).
Test fails without the change, passes with it, along with every other test in test_shutil.
Some variants were experimented with to make the one line change and the most performant one was picked.
# Added Test for PathLike directory destination, the current fail case
```
Lib/test/test_shutil.py::TestMove::test_move_file_pathlike FAILED [100%]
============================================================== FAILURES ===============================================================
__________________________________________________ TestMove.test_move_file_pathlike ___________________________________________________
self = <test.test_shutil.TestMove testMethod=test_move_file_pathlike>
def test_move_file_pathlike(self):
# Move a file to another location on the same filesystem.
src = pathlib.Path(self.src_file)
> self._check_move_file(src, self.dst_dir, self.dst_file)
Lib/test/test_shutil.py:1563:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Lib/test/test_shutil.py:1545: in _check_move_file
shutil.move(src, dst)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py:562: in move
real_dst = os.path.join(dst, _basename(src))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
path = PosixPath('/var/folders/r2/psq74t5x3nbfzlph8bh2pvdw0000gn/T/tmp9ie0wh9_/foo')
def _basename(path):
# A basename() variant which first strips the trailing slash, if present.
# Thus we always get the last component of the path, even for directories.
sep = os.path.sep + (os.path.altsep or '')
> return os.path.basename(path.rstrip(sep))
E AttributeError: 'PosixPath' object has no attribute 'rstrip'
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py:526: AttributeError
============================================== 1 failed, 102 deselected in 0.30 seconds ===============================================
```
After change:
```
========================================================= test session starts =========================================================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- /Users/maxwellmckinnon/.venvs/TA3.7/bin/python3.7
cachedir: .pytest_cache
rootdir: /Users/maxwellmckinnon/dev/cpython
plugins: cov-2.7.1, mock-1.10.4
collected 103 items / 102 deselected / 1 selected
Lib/test/test_shutil.py::TestMove::test_move_file_pathlike PASSED [100%]
============================================== 1 passed, 102 deselected in 0.06 seconds ===============================================
```
Running all the tests in test_shutil.py
```
╰─ pytest Lib/test/test_shutil.py -v
========================================================= test session starts =========================================================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- /Users/maxwellmckinnon/.venvs/TA3.7/bin/python3.7
cachedir: .pytest_cache
rootdir: /Users/maxwellmckinnon/dev/cpython
plugins: cov-2.7.1, mock-1.10.4
collected 103 items
Lib/test/test_shutil.py::TestShutil::test_chown PASSED [ 0%]
Lib/test/test_shutil.py::TestShutil::test_copy PASSED [ 1%]
...
Lib/test/test_shutil.py::TermsizeTests::test_stty_match SKIPPED [ 99%]
Lib/test/test_shutil.py::PublicAPITests::test_module_all_attribute PASSED [100%]
================================================ 96 passed, 7 skipped in 1.25 seconds =================================================
```
# Performance Considerations
Is it considered poor form to get rid of _basename altogether and make use of pathlib in the move function? I'm not sure if the idea is for all these modules to strictly avoid circular dependencies. They are already using os.path which is just as much a citizen in 3.8 as pathlib right?
e.g.
`real_dst = os.path.join(dst, _basename(src))`
becomes
`real_dst = Path(dst) / Path(src).name`
I've looked around and familiarized myself, and I now think importing pathlib here is fine. My only remaining concern is that of performance.
Here's the performance difference for this step.
```
In [46]: %timeit real_dst = os.path.join("a/b/c", _basename('b/'))
2.71 µs ± 62.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [47]: %timeit real_dst = Path("a/b/c") / Path('b/').name
12.4 µs ± 65.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
```
Is 10us significant or insignificant compared to the least expensive operation this function will do? I don't know. Let's find out.
```
In [55]: %timeit os.rename('/tmp/a/a.txt', '/tmp/a/b.txt'); os.rename('/tmp/a/b.txt', '/tmp/a/a.txt')
124 µs ± 2.18 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
```
62us to rename. 10us seems significant enough that we wouldn't want to favor the Path sugar suggestion. 16% speed decrease from adding the 10us.
What do people think? I was hoping to get to use pathlib.Path here, but I suspect for this low level move, it should be as fast as possible, and 16% is not worth one line of sugary code to me.
https://bugs.python.org/issue32689
Automerge-Triggered-By: @gvanrossum
2019-09-30 23:41:16 -03:00
|
|
|
|
.. versionchanged:: 3.9
|
|
|
|
|
Accepts a :term:`path-like object` for both *src* and *dst*.
|
|
|
|
|
|
2011-07-01 08:55:36 -03:00
|
|
|
|
.. function:: disk_usage(path)
|
|
|
|
|
|
2011-08-08 11:51:11 -03:00
|
|
|
|
Return disk usage statistics about the given path as a :term:`named tuple`
|
|
|
|
|
with the attributes *total*, *used* and *free*, which are the amount of
|
2018-09-25 11:57:36 -03:00
|
|
|
|
total, used and free space, in bytes. *path* may be a file or a
|
|
|
|
|
directory.
|
2011-07-01 08:55:36 -03:00
|
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
|
2018-09-25 11:57:36 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
On Windows, *path* can now be a file or directory.
|
|
|
|
|
|
2018-10-12 11:55:20 -03:00
|
|
|
|
.. availability:: Unix, Windows.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2011-08-22 18:28:27 -03:00
|
|
|
|
.. function:: chown(path, user=None, group=None)
|
|
|
|
|
|
|
|
|
|
Change owner *user* and/or *group* of the given *path*.
|
|
|
|
|
|
|
|
|
|
*user* can be a system user name or a uid; the same applies to *group*. At
|
|
|
|
|
least one argument is required.
|
|
|
|
|
|
|
|
|
|
See also :func:`os.chown`, the underlying function.
|
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.chown path,user,group shutil.chown
|
|
|
|
|
|
2018-10-12 11:55:20 -03:00
|
|
|
|
.. availability:: Unix.
|
2011-08-22 18:28:27 -03:00
|
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
|
2012-06-24 12:37:07 -03:00
|
|
|
|
|
2012-06-22 18:00:30 -03:00
|
|
|
|
.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)
|
|
|
|
|
|
2012-06-24 12:37:07 -03:00
|
|
|
|
Return the path to an executable which would be run if the given *cmd* was
|
|
|
|
|
called. If no *cmd* would be called, return ``None``.
|
2012-06-22 18:00:30 -03:00
|
|
|
|
|
2016-04-17 02:32:47 -03:00
|
|
|
|
*mode* is a permission mask passed to :func:`os.access`, by default
|
2012-06-22 18:00:30 -03:00
|
|
|
|
determining if the file exists and executable.
|
|
|
|
|
|
2012-06-24 12:37:07 -03:00
|
|
|
|
When no *path* is specified, the results of :func:`os.environ` are used,
|
|
|
|
|
returning either the "PATH" value or a fallback of :attr:`os.defpath`.
|
2012-06-22 18:00:30 -03:00
|
|
|
|
|
2012-06-24 12:37:07 -03:00
|
|
|
|
On Windows, the current directory is always prepended to the *path* whether
|
|
|
|
|
or not you use the default or provide your own, which is the behavior the
|
2014-05-20 13:58:38 -03:00
|
|
|
|
command shell uses when finding executables. Additionally, when finding the
|
2012-06-24 12:37:07 -03:00
|
|
|
|
*cmd* in the *path*, the ``PATHEXT`` environment variable is checked. For
|
|
|
|
|
example, if you call ``shutil.which("python")``, :func:`which` will search
|
|
|
|
|
``PATHEXT`` to know that it should look for ``python.exe`` within the *path*
|
|
|
|
|
directories. For example, on Windows::
|
2012-06-22 18:00:30 -03:00
|
|
|
|
|
2012-06-24 12:37:07 -03:00
|
|
|
|
>>> shutil.which("python")
|
2013-01-22 04:31:36 -04:00
|
|
|
|
'C:\\Python33\\python.EXE'
|
2012-06-22 18:00:30 -03:00
|
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
2011-08-22 18:28:27 -03:00
|
|
|
|
|
2019-02-13 07:25:10 -04:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
The :class:`bytes` type is now accepted. If *cmd* type is
|
|
|
|
|
:class:`bytes`, the result type is also :class:`bytes`.
|
2012-06-24 12:37:07 -03:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. exception:: Error
|
|
|
|
|
|
2011-07-28 17:49:11 -03:00
|
|
|
|
This exception collects exceptions that are raised during a multi-file
|
|
|
|
|
operation. For :func:`copytree`, the exception argument is a list of 3-tuples
|
|
|
|
|
(*srcname*, *dstname*, *exception*).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
.. _shutil-platform-dependent-efficient-copy-operations:
|
|
|
|
|
|
|
|
|
|
Platform-dependent efficient copy operations
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Starting from Python 3.8 all functions involving a file copy (:func:`copyfile`,
|
|
|
|
|
:func:`copy`, :func:`copy2`, :func:`copytree`, and :func:`move`) may use
|
|
|
|
|
platform-specific "fast-copy" syscalls in order to copy the file more
|
|
|
|
|
efficiently (see :issue:`33671`).
|
|
|
|
|
"fast-copy" means that the copying operation occurs within the kernel, avoiding
|
|
|
|
|
the use of userspace buffers in Python as in "``outfd.write(infd.read())``".
|
|
|
|
|
|
2018-06-19 12:27:29 -03:00
|
|
|
|
On macOS `fcopyfile`_ is used to copy the file content (not metadata).
|
2018-06-12 18:04:50 -03:00
|
|
|
|
|
2019-05-30 03:05:41 -03:00
|
|
|
|
On Linux :func:`os.sendfile` is used.
|
2018-06-12 18:04:50 -03:00
|
|
|
|
|
2018-06-19 12:27:29 -03:00
|
|
|
|
On Windows :func:`shutil.copyfile` uses a bigger default buffer size (1 MiB
|
2019-03-02 00:31:01 -04:00
|
|
|
|
instead of 64 KiB) and a :func:`memoryview`-based variant of
|
2018-06-19 12:27:29 -03:00
|
|
|
|
:func:`shutil.copyfileobj` is used.
|
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
If the fast-copy operation fails and no data was written in the destination
|
|
|
|
|
file then shutil will silently fallback on using less efficient
|
|
|
|
|
:func:`copyfileobj` function internally.
|
|
|
|
|
|
|
|
|
|
.. versionchanged:: 3.8
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-01-16 11:55:55 -04:00
|
|
|
|
.. _shutil-copytree-example:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-02-23 01:30:31 -04:00
|
|
|
|
copytree example
|
2012-06-24 13:09:40 -03:00
|
|
|
|
~~~~~~~~~~~~~~~~
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
This example is the implementation of the :func:`copytree` function, described
|
|
|
|
|
above, with the docstring omitted. It demonstrates many of the other functions
|
|
|
|
|
provided by this module. ::
|
|
|
|
|
|
|
|
|
|
def copytree(src, dst, symlinks=False):
|
|
|
|
|
names = os.listdir(src)
|
|
|
|
|
os.makedirs(dst)
|
|
|
|
|
errors = []
|
|
|
|
|
for name in names:
|
|
|
|
|
srcname = os.path.join(src, name)
|
|
|
|
|
dstname = os.path.join(dst, name)
|
|
|
|
|
try:
|
|
|
|
|
if symlinks and os.path.islink(srcname):
|
|
|
|
|
linkto = os.readlink(srcname)
|
|
|
|
|
os.symlink(linkto, dstname)
|
|
|
|
|
elif os.path.isdir(srcname):
|
|
|
|
|
copytree(srcname, dstname, symlinks)
|
|
|
|
|
else:
|
|
|
|
|
copy2(srcname, dstname)
|
|
|
|
|
# XXX What about devices, sockets etc.?
|
2012-12-15 16:59:24 -04:00
|
|
|
|
except OSError as why:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
errors.append((srcname, dstname, str(why)))
|
|
|
|
|
# catch the Error from the recursive copytree so that we can
|
|
|
|
|
# continue with other files
|
|
|
|
|
except Error as err:
|
|
|
|
|
errors.extend(err.args[0])
|
|
|
|
|
try:
|
|
|
|
|
copystat(src, dst)
|
|
|
|
|
except OSError as why:
|
2012-12-19 08:33:35 -04:00
|
|
|
|
# can't copy file access times on Windows
|
|
|
|
|
if why.winerror is None:
|
|
|
|
|
errors.extend((src, dst, str(why)))
|
2007-08-15 11:28:22 -03:00
|
|
|
|
if errors:
|
2007-09-01 20:34:30 -03:00
|
|
|
|
raise Error(errors)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-02-23 01:30:31 -04:00
|
|
|
|
Another example that uses the :func:`ignore_patterns` helper::
|
|
|
|
|
|
|
|
|
|
from shutil import copytree, ignore_patterns
|
|
|
|
|
|
|
|
|
|
copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
|
|
|
|
|
|
|
|
|
|
This will copy everything except ``.pyc`` files and files or directories whose
|
|
|
|
|
name starts with ``tmp``.
|
|
|
|
|
|
|
|
|
|
Another example that uses the *ignore* argument to add a logging call::
|
|
|
|
|
|
|
|
|
|
from shutil import copytree
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
def _logpath(path, names):
|
2016-08-31 04:22:29 -03:00
|
|
|
|
logging.info('Working in %s', path)
|
2010-02-23 01:30:31 -04:00
|
|
|
|
return [] # nothing will be ignored
|
|
|
|
|
|
|
|
|
|
copytree(source, destination, ignore=_logpath)
|
|
|
|
|
|
|
|
|
|
|
2014-05-07 14:05:45 -03:00
|
|
|
|
.. _shutil-rmtree-example:
|
|
|
|
|
|
|
|
|
|
rmtree example
|
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
This example shows how to remove a directory tree on Windows where some
|
|
|
|
|
of the files have their read-only bit set. It uses the onerror callback
|
|
|
|
|
to clear the readonly bit and reattempt the remove. Any subsequent failure
|
|
|
|
|
will propagate. ::
|
|
|
|
|
|
|
|
|
|
import os, stat
|
|
|
|
|
import shutil
|
2014-05-07 14:08:08 -03:00
|
|
|
|
|
2014-05-07 14:05:45 -03:00
|
|
|
|
def remove_readonly(func, path, _):
|
|
|
|
|
"Clear the readonly bit and reattempt the removal"
|
|
|
|
|
os.chmod(path, stat.S_IWRITE)
|
2014-05-07 14:08:08 -03:00
|
|
|
|
func(path)
|
|
|
|
|
|
2014-05-07 14:05:45 -03:00
|
|
|
|
shutil.rmtree(directory, onerror=remove_readonly)
|
|
|
|
|
|
2011-01-23 07:29:08 -04:00
|
|
|
|
.. _archiving-operations:
|
|
|
|
|
|
|
|
|
|
Archiving operations
|
|
|
|
|
--------------------
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
2012-06-24 13:09:40 -03:00
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
2016-12-16 12:58:33 -04:00
|
|
|
|
.. versionchanged:: 3.5
|
|
|
|
|
Added support for the *xztar* format.
|
|
|
|
|
|
|
|
|
|
|
2012-01-16 11:55:55 -04:00
|
|
|
|
High-level utilities to create and read compressed and archived files are also
|
|
|
|
|
provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
|
|
|
|
|
|
2010-02-23 01:30:31 -04:00
|
|
|
|
.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
|
|
|
|
|
|
2011-01-23 07:29:08 -04:00
|
|
|
|
Create an archive file (such as zip or tar) and return its name.
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
*base_name* is the name of the file to create, including the path, minus
|
|
|
|
|
any format-specific extension. *format* is the archive format: one of
|
2016-12-16 12:58:33 -04:00
|
|
|
|
"zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the
|
|
|
|
|
:mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is
|
|
|
|
|
available), or "xztar" (if the :mod:`lzma` module is available).
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
*root_dir* is a directory that will be the root directory of the
|
2020-06-08 02:01:21 -03:00
|
|
|
|
archive, all paths in the archive will be relative to it; for example,
|
|
|
|
|
we typically chdir into *root_dir* before creating the archive.
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
*base_dir* is the directory where we start archiving from;
|
2010-04-20 08:26:51 -03:00
|
|
|
|
i.e. *base_dir* will be the common prefix of all files and
|
2020-06-08 02:01:21 -03:00
|
|
|
|
directories in the archive. *base_dir* must be given relative
|
|
|
|
|
to *root_dir*. See :ref:`shutil-archiving-example-with-basedir` for how to
|
|
|
|
|
use *base_dir* and *root_dir* together.
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
*root_dir* and *base_dir* both default to the current directory.
|
|
|
|
|
|
2014-10-31 06:02:40 -03:00
|
|
|
|
If *dry_run* is true, no archive is created, but the operations that would be
|
|
|
|
|
executed are logged to *logger*.
|
|
|
|
|
|
2010-02-23 01:30:31 -04:00
|
|
|
|
*owner* and *group* are used when creating a tar archive. By default,
|
|
|
|
|
uses the current owner and group.
|
|
|
|
|
|
2011-11-07 12:31:07 -04:00
|
|
|
|
*logger* must be an object compatible with :pep:`282`, usually an instance of
|
|
|
|
|
:class:`logging.Logger`.
|
2011-01-23 07:29:08 -04:00
|
|
|
|
|
2014-10-31 06:54:06 -03:00
|
|
|
|
The *verbose* argument is unused and deprecated.
|
2014-10-31 06:02:40 -03:00
|
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
|
.. audit-event:: shutil.make_archive base_name,format,root_dir,base_dir shutil.make_archive
|
2019-06-24 12:42:54 -03:00
|
|
|
|
|
2019-04-07 01:47:49 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
The modern pax (POSIX.1-2001) format is now used instead of
|
|
|
|
|
the legacy GNU format for archives created with ``format="tar"``.
|
|
|
|
|
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
.. function:: get_archive_formats()
|
|
|
|
|
|
2011-07-28 17:49:11 -03:00
|
|
|
|
Return a list of supported formats for archiving.
|
2015-10-10 07:36:22 -03:00
|
|
|
|
Each element of the returned sequence is a tuple ``(name, description)``.
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
By default :mod:`shutil` provides these formats:
|
|
|
|
|
|
2016-12-16 12:58:33 -04:00
|
|
|
|
- *zip*: ZIP file (if the :mod:`zlib` module is available).
|
2019-04-07 01:47:49 -03:00
|
|
|
|
- *tar*: Uncompressed tar file. Uses POSIX.1-2001 pax format for new archives.
|
2016-12-16 12:58:33 -04:00
|
|
|
|
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
|
|
|
|
|
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
|
|
|
|
|
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
You can register new formats or provide your own archiver for any existing
|
|
|
|
|
formats, by using :func:`register_archive_format`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: register_archive_format(name, function, [extra_args, [description]])
|
|
|
|
|
|
2014-10-31 06:02:40 -03:00
|
|
|
|
Register an archiver for the format *name*.
|
|
|
|
|
|
|
|
|
|
*function* is the callable that will be used to unpack archives. The callable
|
|
|
|
|
will receive the *base_name* of the file to create, followed by the
|
|
|
|
|
*base_dir* (which defaults to :data:`os.curdir`) to start archiving from.
|
|
|
|
|
Further arguments are passed as keyword arguments: *owner*, *group*,
|
|
|
|
|
*dry_run* and *logger* (as passed in :func:`make_archive`).
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
2011-01-23 07:29:08 -04:00
|
|
|
|
If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be
|
2010-02-23 01:30:31 -04:00
|
|
|
|
used as extra keywords arguments when the archiver callable is used.
|
|
|
|
|
|
|
|
|
|
*description* is used by :func:`get_archive_formats` which returns the
|
2014-10-31 06:02:40 -03:00
|
|
|
|
list of archivers. Defaults to an empty string.
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
|
2010-04-28 14:51:36 -03:00
|
|
|
|
.. function:: unregister_archive_format(name)
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
Remove the archive format *name* from the list of supported formats.
|
|
|
|
|
|
|
|
|
|
|
2010-04-28 14:51:36 -03:00
|
|
|
|
.. function:: unpack_archive(filename[, extract_dir[, format]])
|
|
|
|
|
|
|
|
|
|
Unpack an archive. *filename* is the full path of the archive.
|
|
|
|
|
|
|
|
|
|
*extract_dir* is the name of the target directory where the archive is
|
|
|
|
|
unpacked. If not provided, the current working directory is used.
|
|
|
|
|
|
2016-12-16 12:58:33 -04:00
|
|
|
|
*format* is the archive format: one of "zip", "tar", "gztar", "bztar", or
|
|
|
|
|
"xztar". Or any other format registered with
|
|
|
|
|
:func:`register_unpack_format`. If not provided, :func:`unpack_archive`
|
|
|
|
|
will use the archive file name extension and see if an unpacker was
|
|
|
|
|
registered for that extension. In case none is found,
|
|
|
|
|
a :exc:`ValueError` is raised.
|
2010-04-28 14:51:36 -03:00
|
|
|
|
|
2020-02-13 03:47:42 -04:00
|
|
|
|
.. audit-event:: shutil.unpack_archive filename,extract_dir,format shutil.unpack_archive
|
|
|
|
|
|
2017-05-05 18:27:12 -03:00
|
|
|
|
.. versionchanged:: 3.7
|
|
|
|
|
Accepts a :term:`path-like object` for *filename* and *extract_dir*.
|
|
|
|
|
|
2010-04-28 14:51:36 -03:00
|
|
|
|
|
2011-01-23 07:29:08 -04:00
|
|
|
|
.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
|
2010-04-28 14:51:36 -03:00
|
|
|
|
|
|
|
|
|
Registers an unpack format. *name* is the name of the format and
|
|
|
|
|
*extensions* is a list of extensions corresponding to the format, like
|
|
|
|
|
``.zip`` for Zip files.
|
|
|
|
|
|
|
|
|
|
*function* is the callable that will be used to unpack archives. The
|
|
|
|
|
callable will receive the path of the archive, followed by the directory
|
|
|
|
|
the archive must be extracted to.
|
|
|
|
|
|
|
|
|
|
When provided, *extra_args* is a sequence of ``(name, value)`` tuples that
|
|
|
|
|
will be passed as keywords arguments to the callable.
|
|
|
|
|
|
|
|
|
|
*description* can be provided to describe the format, and will be returned
|
|
|
|
|
by the :func:`get_unpack_formats` function.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: unregister_unpack_format(name)
|
|
|
|
|
|
|
|
|
|
Unregister an unpack format. *name* is the name of the format.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: get_unpack_formats()
|
|
|
|
|
|
|
|
|
|
Return a list of all registered formats for unpacking.
|
|
|
|
|
Each element of the returned sequence is a tuple
|
|
|
|
|
``(name, extensions, description)``.
|
|
|
|
|
|
|
|
|
|
By default :mod:`shutil` provides these formats:
|
|
|
|
|
|
2016-12-17 21:23:09 -04:00
|
|
|
|
- *zip*: ZIP file (unpacking compressed files works only if the corresponding
|
2016-12-16 12:58:33 -04:00
|
|
|
|
module is available).
|
|
|
|
|
- *tar*: uncompressed tar file.
|
|
|
|
|
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
|
|
|
|
|
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
|
|
|
|
|
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
|
2010-04-28 14:51:36 -03:00
|
|
|
|
|
|
|
|
|
You can register new formats or provide your own unpacker for any existing
|
|
|
|
|
formats, by using :func:`register_unpack_format`.
|
|
|
|
|
|
|
|
|
|
|
2012-01-16 11:55:55 -04:00
|
|
|
|
.. _shutil-archiving-example:
|
2010-04-28 14:51:36 -03:00
|
|
|
|
|
2010-02-23 01:30:31 -04:00
|
|
|
|
Archiving example
|
2012-06-24 13:09:40 -03:00
|
|
|
|
~~~~~~~~~~~~~~~~~
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
In this example, we create a gzip'ed tar-file archive containing all files
|
|
|
|
|
found in the :file:`.ssh` directory of the user::
|
|
|
|
|
|
|
|
|
|
>>> from shutil import make_archive
|
|
|
|
|
>>> import os
|
|
|
|
|
>>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
|
|
|
|
|
>>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
|
|
|
|
|
>>> make_archive(archive_name, 'gztar', root_dir)
|
|
|
|
|
'/Users/tarek/myarchive.tar.gz'
|
|
|
|
|
|
2016-07-26 06:18:21 -03:00
|
|
|
|
The resulting archive contains:
|
|
|
|
|
|
|
|
|
|
.. code-block:: shell-session
|
2010-02-23 01:30:31 -04:00
|
|
|
|
|
|
|
|
|
$ tar -tzvf /Users/tarek/myarchive.tar.gz
|
|
|
|
|
drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
|
|
|
|
|
-rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
|
|
|
|
|
-rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
|
|
|
|
|
-rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
|
|
|
|
|
-rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
|
|
|
|
|
-rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
|
|
|
|
|
-rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
|
|
|
|
|
-rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
|
2012-02-08 18:28:36 -04:00
|
|
|
|
|
|
|
|
|
|
2020-06-08 02:01:21 -03:00
|
|
|
|
.. _shutil-archiving-example-with-basedir:
|
|
|
|
|
|
|
|
|
|
Archiving example with *base_dir*
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
In this example, similar to the `one above <shutil-archiving-example_>`_,
|
|
|
|
|
we show how to use :func:`make_archive`, but this time with the usage of
|
|
|
|
|
*base_dir*. We now have the following directory structure:
|
|
|
|
|
|
|
|
|
|
.. code-block:: shell-session
|
|
|
|
|
|
|
|
|
|
$ tree tmp
|
|
|
|
|
tmp
|
|
|
|
|
└── root
|
|
|
|
|
└── structure
|
|
|
|
|
├── content
|
|
|
|
|
└── please_add.txt
|
|
|
|
|
└── do_not_add.txt
|
|
|
|
|
|
|
|
|
|
In the final archive, :file:`please_add.txt` should be included, but
|
|
|
|
|
:file:`do_not_add.txt` should not. Therefore we use the following::
|
|
|
|
|
|
|
|
|
|
>>> from shutil import make_archive
|
|
|
|
|
>>> import os
|
|
|
|
|
>>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
|
|
|
|
|
>>> make_archive(
|
|
|
|
|
... archive_name,
|
|
|
|
|
... 'tar',
|
|
|
|
|
... root_dir='tmp/root',
|
|
|
|
|
... base_dir='structure/content',
|
|
|
|
|
... )
|
|
|
|
|
'/Users/tarek/my_archive.tar'
|
|
|
|
|
|
|
|
|
|
Listing the files in the resulting archive gives us:
|
|
|
|
|
|
|
|
|
|
.. code-block:: shell-session
|
|
|
|
|
|
|
|
|
|
$ python -m tarfile -l /Users/tarek/myarchive.tar
|
|
|
|
|
structure/content/
|
|
|
|
|
structure/content/please_add.txt
|
|
|
|
|
|
|
|
|
|
|
2012-02-08 18:28:36 -04:00
|
|
|
|
Querying the size of the output terminal
|
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
|
|
.. function:: get_terminal_size(fallback=(columns, lines))
|
|
|
|
|
|
|
|
|
|
Get the size of the terminal window.
|
|
|
|
|
|
|
|
|
|
For each of the two dimensions, the environment variable, ``COLUMNS``
|
|
|
|
|
and ``LINES`` respectively, is checked. If the variable is defined and
|
|
|
|
|
the value is a positive integer, it is used.
|
|
|
|
|
|
|
|
|
|
When ``COLUMNS`` or ``LINES`` is not defined, which is the common case,
|
|
|
|
|
the terminal connected to :data:`sys.__stdout__` is queried
|
|
|
|
|
by invoking :func:`os.get_terminal_size`.
|
|
|
|
|
|
|
|
|
|
If the terminal size cannot be successfully queried, either because
|
|
|
|
|
the system doesn't support querying, or because we are not
|
|
|
|
|
connected to a terminal, the value given in ``fallback`` parameter
|
|
|
|
|
is used. ``fallback`` defaults to ``(80, 24)`` which is the default
|
|
|
|
|
size used by many terminal emulators.
|
|
|
|
|
|
|
|
|
|
The value returned is a named tuple of type :class:`os.terminal_size`.
|
|
|
|
|
|
|
|
|
|
See also: The Single UNIX Specification, Version 2,
|
|
|
|
|
`Other Environment Variables`_.
|
|
|
|
|
|
2016-12-27 08:09:11 -04:00
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
|
2018-06-12 18:04:50 -03:00
|
|
|
|
.. _`fcopyfile`:
|
|
|
|
|
http://www.manpagez.com/man/3/copyfile/
|
|
|
|
|
|
2012-02-08 18:28:36 -04:00
|
|
|
|
.. _`Other Environment Variables`:
|
|
|
|
|
http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003
|