8000 gh-88513: clarify shutil.copytree's dirs_exist_ok arg (GH-91434) (GH-… · python/cpython@289f27d · GitHub
[go: up one dir, main page]

Skip to content

Commit 289f27d

Browse files
gh-88513: clarify shutil.copytree's dirs_exist_ok arg (GH-91434) (GH-91464)
* add a paragraph to document this kwarg in detail * update docstring in the source accordingly (cherry picked from commit f33e2c8) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent de7b756 commit 289f27d

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

Doc/library/shutil.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,8 @@ Directory and files operations
230230
dirs_exist_ok=False)
231231

232232
Recursively copy an entire directory tree rooted at *src* to a directory
233-
named *dst* and return the destination directory. *dirs_exist_ok* dictates
234-
whether to raise an exception in case *dst* or any missing parent directory
235-
already exists.
233+
named *dst* and return the destination directory. All intermediate
234+
directories needed to contain *dst* will also be created by default.
236235

237236
Permissions and times of directories are copied with :func:`copystat`,
238237
individual files are copied using :func:`~shutil.copy2`.
@@ -263,8 +262,14 @@ Directory and files operations
263262

264263
If *copy_function* is given, it must be a callable that will be used to copy
265264
each file. It will be called with the source path and the destination path
266-
  as arguments. By default, :func:`~shutil.copy2` is used, but any function
267-
  that supports the same signature (like :func:`~shutil.copy`) can be used.
265+
as arguments. By default, :func:`~shutil.copy2` is used, but any function
266+
that supports the same signature (like :func:`~shutil.copy`) can be used.
267+
268+
If *dirs_exist_ok* is false (the default) and *dst* already exists, a
269+
:exc:`FileExistsError` is raised. If *dirs_exist_ok* is true, the copying
270+
operation will continue if it encounters existing directories, and files
271+
within the *dst* tree will be overwritten by corresponding files from the
272+
*src* tree.
268273

269274
.. audit-event:: shutil.copytree src,dst shutil.copytree
270275

@@ -275,7 +280,7 @@ Directory and files operations
275280
.. versionchanged:: 3.2
276281
Added the *copy_function* argument to be able to provide a custom copy
277282
function.
278-
Added the *ignore_dangling_symlinks* argument to silent dangling symlinks
283+
Added the *ignore_dangling_symlinks* argument to silence dangling symlinks
279284
errors when *symlinks* is false.
280285

281286
.. versionchanged:: 3.8

Lib/shutil.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,6 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
516516
ignore_dangling_symlinks=False, dirs_exist_ok=False):
517517
"""Recursively copy a directory tree and return the destination directory.
518518
519-
dirs_exist_ok dictates whether to raise an exception in case dst or any
520-
missing parent directory already exists.
521-
522519
If exception(s) occur, an Error is raised with a list of reasons.
523520
524521
If the optional symlinks flag is true, symbolic links in the
@@ -549,6 +546,11 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
549546
destination path as arguments. By default, copy2() is used, but any
550547
function that supports the same signature (like copy()) can be used.
551548
549+
If dirs_exist_ok is false (the default) and `dst` already exists, a
550+
`FileExistsError` is raised. If `dirs_exist_ok` is true, the copying
551+
operation will continue if it encounters existing directories, and files
552+
within the `dst` tree will be overwritten by corresponding files from the
553+
`src` tree.
552554
"""
553555
sys.audit("shutil.copytree", src, dst)
554556
with os.scandir(src) as itr:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify the meaning of *dirs_exist_ok*, a kwarg of :func:`shutil.copytree`.

0 commit comments

Comments
 (0)
0