GH-73991: Add pathlib.Path.rmtree() (#119060)

Add a `Path.rmtree()` method that removes an entire directory tree, like
`shutil.rmtree()`. The signature of the optional *on_error* argument
matches the `Path.walk()` argument of the same name, but differs from the
*onexc* and *onerror* arguments to `shutil.rmtree()`. Consistency within
pathlib is probably more important.

In the private pathlib ABCs, we add an implementation based on `walk()`.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Barney Gale 2024-07-20 21:14:13 +01:00 committed by GitHub
parent 8db5f48007
commit 094375b9b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 448 additions and 5 deletions

View file

@ -830,6 +830,25 @@ class Path(PathBase, PurePath):
"""
os.rmdir(self)
def rmtree(self, ignore_errors=False, on_error=None):
"""
Recursively delete this directory tree.
If *ignore_errors* is true, exceptions raised from scanning the tree
and removing files and directories are ignored. Otherwise, if
*on_error* is set, it will be called to handle the error. If neither
*ignore_errors* nor *on_error* are set, exceptions are propagated to
the caller.
"""
if on_error:
def onexc(func, filename, err):
err.filename = filename
on_error(err)
else:
onexc = None
import shutil
shutil.rmtree(str(self), ignore_errors, onexc=onexc)
def rename(self, target):
"""
Rename this path to the target path.