[issue41737] Improper NotADirectoryError when opening a file under a fake directory

Danny Lin report at bugs.python.org
Tue Sep 8 06:24:50 EDT 2020


Danny Lin <danny0838 at gmail.com> added the comment:

I'm not so familiar about the spec. If such behavior is confirmed due to implementation difference across OSes, and it's also not desirable to change the mapping of the OS error to Python exception, we can simplify left it as-is.

However, this behavior difference can potentially cause cross-platform compatibility. For example, the code:

    try:
        open('/path/to/file/somename.txt')
    except FileNotFoundError:
        """do something"""

would work on Windows but break on Linux (or POSIX?).

The current (3.8) description for exception NotADirectoryError is:

    Raised when a directory operation (such as os.listdir()) is requested on something which is not a directory. Corresponds to errno ENOTDIR.

According to this, a user probably won't expect to receive a NotADirectoryError for open('/path/to/file/somename.txt'), as this doesn't seem like a directory operation at all, unless he is expert enough to know about the implication of errno ENOTDIR.

I think a note should at least be added to the documentation if we are not going to change the behavior.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41737>
_______________________________________


More information about the Python-bugs-list mailing list