You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
os.remove is documented to raise IsADirectoryError but macOS raises PermissionError #95815
Remove (delete) the file path. If path is a directory, an IsADirectoryError is raised. Use rmdir() to remove directories. If the file does not exist, a FileNotFoundError is raised.
This is the case on Linux:
$ python -c 'import os, tempfile; os.remove(tempfile.mkdtemp())'
Traceback (most recent call last):
File "<string>", line 1, in<module>
IsADirectoryError: [Errno 21] Is a directory: '/tmp/tmphxrd1p6n'
However on macOS this results in a PermissionError:
In Windows, calling os.unlink() or os.remove() on a directory also raises PermissionError. It can be clarified by checking the last status value from the internal NT API. Unfortunately, Microsoft doesn't document either the function to return this value or when it's valid. For example:
I've added a PR that reverts the text back to mentioning OSError instead of IsADirectoryError, but I'm not convinced yet that this is the best way to change the documentation. An alternative is to document both exceptions.
I'm -1 on changing the implementation to raise the documented exception, that's not possible to do without a race condition because there are other reasons for getting EPERM (e.g. the folder in which the to-be-removed object is located is not writable).
Activity
eryksun commentedon Aug 12, 2022
In Windows, calling
os.unlink()oros.remove()on a directory also raisesPermissionError. It can be clarified by checking the last status value from the internal NT API. Unfortunately, Microsoft doesn't document either the function to return this value or when it's valid. For example:ronaldoussoren commentedon Nov 15, 2022
The behaviour on macOS matches the documentation of the underlying os, the manpage for unlink(2) and unlinkat(2) mention:
IMHO we shouldn't try to work around this, but should mention this in the documentation.
ronaldoussoren commentedon Nov 16, 2022
The current text was introduced in #14262, replacing OSError in the text. In this case the new more specific exception is incorrect.
pythonGH-95815: Document less specific error for os.remove
ronaldoussoren commentedon Nov 18, 2022
I've added a PR that reverts the text back to mentioning OSError instead of IsADirectoryError, but I'm not convinced yet that this is the best way to change the documentation. An alternative is to document both exceptions.
I'm -1 on changing the implementation to raise the documented exception, that's not possible to do without a race condition because there are other reasons for getting EPERM (e.g. the folder in which the to-be-removed object is located is not writable).
GH-95815: Document less specific error for os.remove (#99571)
pythonGH-95815: Document less specific error for os.remove (pythonGH-…
pythonGH-95815: Document less specific error for os.remove (pythonGH-…
[3.11] GH-95815: Document less specific error for os.remove (GH-99571) (
[3.10] GH-95815: Document less specific error for os.remove (GH-99571) (