Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
GH-73991: Add pathlib.Path.rmtree()#119060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
23 commits Select commit Hold shift + click to select a range
bbcb1ce GH-73991: Add `pathlib.Path.rmtree()`
barneygale 94ef5c1 Merge branch 'main' into gh-73991-rmtree
barneygale 62f7eae Use `os.walk()` and `os.fwalk()` rather than `shutil.rmtree()`
barneygale 4c73a92 Undo shutil change
barneygale 98fa2e2 Line up implementations
barneygale 7819005 Merge branch 'main' into gh-73991-rmtree
barneygale b504019 Tests
barneygale bdd1255 Merge branch 'main' into gh-73991-rmtree
barneygale 5d2472e Merge branch 'main' into gh-73991-rmtree
barneygale eadbbe6 Merge branch 'main' into gh-73991-rmtree
barneygale ab7b086 Use implementation from shutil
barneygale f88fc12 Fix some Windows tests.
barneygale 5a0fc07 More Windows test tweaks
barneygale 2559e6a This is confusing :/
barneygale 5588e1f Merge branch 'main' into gh-73991-rmtree
barneygale bcff053 WIP
barneygale be00bfe Merge branch 'main' into gh-73991-rmtree
barneygale d8c4291 Fix docs
barneygale a9c8d4f Minor tweaks
barneygale 4bcc143 Apply suggestions from code review
barneygale ab0f447 Address review feedback.
barneygale 40ed5c8 Merge branch 'main' into gh-73991-rmtree
barneygale 0cf0180 Merge branch 'main' into gh-73991-rmtree
AA-Turner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -915,6 +915,47 @@ def rmdir(self): | ||
| """ | ||
| raise UnsupportedOperation(self._unsupported_msg('rmdir()')) | ||
| 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 ignore_errors: | ||
| def on_error(err): | ||
| pass | ||
| elif on_error is None: | ||
| def on_error(err): | ||
| raise err | ||
| try: | ||
| if self.is_symlink(): | ||
| raise OSError("Cannot call rmtree on a symbolic link") | ||
| elif self.is_junction(): | ||
| raise OSError("Cannot call rmtree on a junction") | ||
| results = self.walk( | ||
| on_error=on_error, | ||
| top_down=False, # Bottom-up so we rmdir() empty directories. | ||
| follow_symlinks=False) | ||
| for dirpath, dirnames, filenames in results: | ||
| for name in filenames: | ||
| try: | ||
| dirpath.joinpath(name).unlink() | ||
| except OSError as err: | ||
| on_error(err) | ||
| for name in dirnames: | ||
| try: | ||
| dirpath.joinpath(name).rmdir() | ||
| except OSError as err: | ||
| on_error(err) | ||
barneygale marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| self.rmdir() | ||
| except OSError as err: | ||
| err.filename = str(self) | ||
| on_error(err) | ||
| def owner(self, *, follow_symlinks=True): | ||
| """ | ||
| Return the login name of the file owner. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.