[New-bugs-announce] [issue41737] Improper NotADirectoryError when opening a file under a fake directory

Danny Lin report at bugs.python.org
Mon Sep 7 11:36:37 EDT 2020


New submission from Danny Lin <danny0838 at gmail.com>:

On Linux (tested on Ubuntu 16.04), if "/path/to/file" is an existing file, the code:

    open('/path/to/file/somename.txt')

raises NotADirectoryError: [Errno 20] Not a directory: '/path/to/file/somename.txt'

On Windows, similar code:

    open(r'C:\path\to\file\somename.txt')

raises FileNotFoundError: [Errno 2] No such file or directory: 'C:\\path\\chrome\\to\\file\\somename.txt'

I think the behavior on Linux is not correct. The user probably cares about the existence of the file to be opened, rather than whether its ancestor directories are valid.

OTOH, if NotADirectoryError should be raised, it should mention '/path/to/file' rather then '/path/to/file/somename.txt'. But what if '/path/to' or '/path' is actually a file? Should it be '/path/to' or '/path' instead for the same reason?

----------
messages: 376505
nosy: danny87105
priority: normal
severity: normal
status: open
title: Improper NotADirectoryError when opening a file under a fake directory
type: behavior
versions: Python 3.8

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


More information about the New-bugs-announce mailing list