Skip to content

gh-145227: Clarify os.remove() directory exception is platform-dependent#145265

Open
mohsinm-dev wants to merge 3 commits intopython:mainfrom
mohsinm-dev:gh-145227-clarify-os-remove-exception
Open

gh-145227: Clarify os.remove() directory exception is platform-dependent#145265
mohsinm-dev wants to merge 3 commits intopython:mainfrom
mohsinm-dev:gh-145227-clarify-os-remove-exception

Conversation

@mohsinm-dev
Copy link
Contributor

@mohsinm-dev mohsinm-dev commented Feb 26, 2026

The OSError subclass raised by os.remove() when the path is a directory depends on the platform and underlying errno:

  • Linux: unlink() returns EISDIRIsADirectoryError
  • macOS: unlink() returns EPERMPermissionError

The documentation previously stated only that OSError is raised, which is technically correct but can mislead users into catching a specific subclass that isn't portable. For example:

try:
    os.remove(path)
except IsADirectoryError:
    os.rmdir(path)

This works on Linux but fails on macOS, where PermissionError is raised instead.

Updated the os.remove() docs to note that the exact subclass is platform-dependent, with IsADirectoryError and PermissionError as examples. No change needed for os.unlink() since its docs already defer to os.remove().


📚 Documentation preview 📚: https://cpython-previews--145265.org.readthedocs.build/

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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! One suggestion: Could you maybe add, on which plattform (for the well known ones) which error is used? Thanks!

Copy link
Contributor

@benediktjohannes benediktjohannes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks, LGTM

Copy link

@LKajan LKajan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows raises a PermissionError as well.

Suggested change
on macOS.
on macOS and Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants