summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-07-20 22:32:52 (GMT)
committerGitHub <noreply@github.com>2024-07-20 22:32:52 (GMT)
commitc4c7097e64b0c9cb0081de8872b90594865c892b (patch)
tree0a0ef494b3df9b78c90f49031318cc0b4f9148a8 /Lib/pathlib
parent094375b9b7e087a4f0f60541dc7f2dc53be92646 (diff)
downloadcpython-c4c7097e64b0c9cb0081de8872b90594865c892b.zip
cpython-c4c7097e64b0c9cb0081de8872b90594865c892b.tar.gz
cpython-c4c7097e64b0c9cb0081de8872b90594865c892b.tar.bz2
GH-73991: Support preserving metadata in `pathlib.Path.copytree()` (#121438)
Add *preserve_metadata* keyword-only argument to `pathlib.Path.copytree()`, defaulting to false. When set to true, we copy timestamps, permissions, extended attributes and flags where available, like `shutil.copystat()`.
Diffstat (limited to 'Lib/pathlib')
-rw-r--r--Lib/pathlib/_abc.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 49e8e4c..c32e776 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -835,7 +835,8 @@ class PathBase(PurePathBase):
if preserve_metadata:
self._copy_metadata(target)
- def copytree(self, target, *, follow_symlinks=True, dirs_exist_ok=False,
+ def copytree(self, target, *, follow_symlinks=True,
+ preserve_metadata=False, dirs_exist_ok=False,
ignore=None, on_error=None):
"""
Recursively copy this directory tree to the given destination.
@@ -851,6 +852,8 @@ class PathBase(PurePathBase):
try:
sources = source_dir.iterdir()
target_dir.mkdir(exist_ok=dirs_exist_ok)
+ if preserve_metadata:
+ source_dir._copy_metadata(target_dir)
for source in sources:
if ignore and ignore(source):
continue
@@ -859,7 +862,8 @@ class PathBase(PurePathBase):
stack.append((source, target_dir.joinpath(source.name)))
else:
source.copy(target_dir.joinpath(source.name),
- follow_symlinks=follow_symlinks)
+ follow_symlinks=follow_symlinks,
+ preserve_metadata=preserve_metadata)
except OSError as err:
on_error(err)
except OSError as err: