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
Add DirectoryNotEmptyError subclass of OSError #77028
I just ran across errno 39 (Directory not empty) when using Path.rename(newdir) when newdir already existed and was not empty. I realized that OSError exceptions don't have a DirectoryNotEmptyError subclass. Maybe we should add one?
Probably not important or common enough to add for 3.7 given we're in beta now.
I don't think that we need to add an *builtin* exception for every single possible errno. On Linux with Python 3.7, I count 133 different error numbers:
>>> len([name for name indir(errno) if name.startswith("E")])
133
PEP-3151 explains the rationale for added OSError subclasses.
I think that we should focus on the most common ones, especially the ones which are available on all platforms.
I have no opinion about "errno 39 (Directory not empty)". Is it a "common" error?
I have no opinion about "errno 39 (Directory not empty)".
Is it a "common" error?
Python's code base never specifically handles ENOTEMPTY. On the other hand, in terms of basic operations on files and directories, I think implementing DirectoryNotEmptyError follows naturally from the existing set: FileNotFoundError, FileExistsError, NotADirectoryError, and IsADirectoryError.
I'm not excited by adding a new builtin symbol: IMHO this module already exists too many symbols.
I'm not convinced that it's worth it. There is exactly 0 line of code in CPython code base which expects ENOTEMPTY errno. So it looks like an artifical use case.
I recall that when PEP-3151 was implemented, the implementation replaced a lot of code using "except OSError as exc: if exc.errno == XXX: ... else: raise" with "except : ..." which was neat.
But again, I don't see any usage of ENOTEMPTY in the Python stdlib.
Are you aware of 3rd party code expecting ENOTEMPTY? How many projects?
Adding a builtin symbol is a significant change, it should be well motived.
Python's code base never specifically handles ENOTEMPTY.
Right, so this use case looks artificial to me.
On the other hand, in terms of basic operations on files and directories, I think implementing DirectoryNotEmptyError follows naturally from the existing set: FileNotFoundError, FileExistsError, NotADirectoryError, and IsADirectoryError.
Again, I consider that we must not add one exception per errno, since the Python errno module contains 133 error codes on Linux. Many are very rare and don't deserve to add a new symbol to the builtins module.
I suggest to close this issue.
If later someone wants to get DirectoryNotEmptyError, please open a new issue with statistics of how many 3rd party projects use ENOTEMPTY.
Activity
warsaw commentedon Feb 15, 2018
I just ran across errno 39 (Directory not empty) when using Path.rename(newdir) when newdir already existed and was not empty. I realized that OSError exceptions don't have a DirectoryNotEmptyError subclass. Maybe we should add one?
Probably not important or common enough to add for 3.7 given we're in beta now.
vstinner commentedon Aug 29, 2019
I don't think that we need to add an *builtin* exception for every single possible errno. On Linux with Python 3.7, I count 133 different error numbers:
PEP-3151 explains the rationale for added OSError subclasses.
I think that we should focus on the most common ones, especially the ones which are available on all platforms.
I have no opinion about "errno 39 (Directory not empty)". Is it a "common" error?
eryksun commentedon Aug 29, 2019
Python's code base never specifically handles ENOTEMPTY. On the other hand, in terms of basic operations on files and directories, I think implementing DirectoryNotEmptyError follows naturally from the existing set: FileNotFoundError, FileExistsError, NotADirectoryError, and IsADirectoryError.
pitrou commentedon Aug 29, 2019
According to POSIX, the only system calls that can fail with ENOTEMPTY are rmdir(), rename() and unlinkat().
vstinner commentedon Sep 12, 2019
Copy of my comment on the review:
#15496 (review)
I'm not excited by adding a new builtin symbol: IMHO this module already exists too many symbols.
I'm not convinced that it's worth it. There is exactly 0 line of code in CPython code base which expects ENOTEMPTY errno. So it looks like an artifical use case.
I recall that when PEP-3151 was implemented, the implementation replaced a lot of code using "except OSError as exc: if exc.errno == XXX: ... else: raise" with "except : ..." which was neat.
But again, I don't see any usage of ENOTEMPTY in the Python stdlib.
Are you aware of 3rd party code expecting ENOTEMPTY? How many projects?
Adding a builtin symbol is a significant change, it should be well motived.
vstinner commentedon Sep 12, 2019
Right, so this use case looks artificial to me.
Again, I consider that we must not add one exception per errno, since the Python errno module contains 133 error codes on Linux. Many are very rare and don't deserve to add a new symbol to the builtins module.
I suggest to close this issue.
If later someone wants to get DirectoryNotEmptyError, please open a new issue with statistics of how many 3rd party projects use ENOTEMPTY.
vstinner commentedon Apr 7, 2026
I close the issue.