gh-145227: Clarify os.remove() directory exception is platform-dependent#145265
Open
mohsinm-dev wants to merge 3 commits intopython:mainfrom
Open
gh-145227: Clarify os.remove() directory exception is platform-dependent#145265mohsinm-dev wants to merge 3 commits intopython:mainfrom
mohsinm-dev wants to merge 3 commits intopython:mainfrom
Conversation
Misc/NEWS.d/next/Documentation/2026-02-26-21-09-18.gh-issue-145227.yVAgmv.rst
Outdated
Show resolved
Hide resolved
Doc/library/os.rst
Outdated
| Remove (delete) the file *path*. If *path* is a directory, an | ||
| :exc:`OSError` is raised. Use :func:`rmdir` to remove directories. | ||
| :exc:`OSError` is raised. The exact exception type depends on the platform: | ||
| for example, :exc:`IsADirectoryError` or :exc:`PermissionError`. |
Contributor
There was a problem hiding this comment.
Looks good to me! One suggestion: Could you maybe add, on which plattform (for the well known ones) which error is used? Thanks!
Contributor
benediktjohannes
left a comment
There was a problem hiding this comment.
Nice! Thanks, LGTM
LKajan
reviewed
Feb 27, 2026
LKajan
left a comment
There was a problem hiding this comment.
I added a comment regarding Windows behavior.
| :exc:`OSError` is raised. Use :func:`rmdir` to remove directories. | ||
| :exc:`OSError` is raised. The exact subclass depends on the platform: | ||
| for example, :exc:`IsADirectoryError` on Linux or :exc:`PermissionError` | ||
| on macOS. |
There was a problem hiding this comment.
Windows raises a PermissionError as well.
Suggested change
| on macOS. | |
| on macOS and Windows. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
OSErrorsubclass raised byos.remove()when the path is a directory depends on the platform and underlying errno:unlink()returnsEISDIR→IsADirectoryErrorunlink()returnsEPERM→PermissionErrorThe documentation previously stated only that
OSErroris raised, which is technically correct but can mislead users into catching a specific subclass that isn't portable. For example:This works on Linux but fails on macOS, where
PermissionErroris raised instead.Updated the
os.remove()docs to note that the exact subclass is platform-dependent, withIsADirectoryErrorandPermissionErroras examples. No change needed foros.unlink()since its docs already defer toos.remove().📚 Documentation preview 📚: https://cpython-previews--145265.org.readthedocs.build/