mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
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:
parent
8db5f48007
commit
094375b9b7
7 changed files with 448 additions and 5 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue