aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/pathlib/_abc.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/pathlib/_abc.py')
-rw-r--r--Lib/pathlib/_abc.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index f1f350a1960..71973913921 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -815,6 +815,36 @@ class PathBase(PurePathBase):
else:
raise
+ def copytree(self, target, *, follow_symlinks=True, dirs_exist_ok=False,
+ ignore=None, on_error=None):
+ """
+ Recursively copy this directory tree to the given destination.
+ """
+ if not isinstance(target, PathBase):
+ target = self.with_segments(target)
+ if on_error is None:
+ def on_error(err):
+ raise err
+ stack = [(self, target)]
+ while stack:
+ source_dir, target_dir = stack.pop()
+ try:
+ sources = source_dir.iterdir()
+ target_dir.mkdir(exist_ok=dirs_exist_ok)
+ for source in sources:
+ if ignore and ignore(source):
+ continue
+ try:
+ if source.is_dir(follow_symlinks=follow_symlinks):
+ stack.append((source, target_dir.joinpath(source.name)))
+ else:
+ source.copy(target_dir.joinpath(source.name),
+ follow_symlinks=follow_symlinks)
+ except OSError as err:
+ on_error(err)
+ except OSError as err:
+ on_error(err)
+
def rename(self, target):
"""
Rename this path to the target path.