Eryk Sun <eryk...@gmail.com> added the comment:

NT filesystems are specified to fail with STATUS_OBJECT_PATH_NOT_FOUND 
(0xC000003A) if a parent component in a path either does not exist or is not a 
directory. In the Windows API, this translates to ERROR_PATH_NOT_FOUND (3), 
which in the C runtime translates to ENOENT (2), which in Python is raised as 
FileNotFoundError.

The design of Path.mkdir() assumes POSIX behavior, which splits the above two 
cases respectively into ENOENT and ENOTDIR. Thus it assumes that 
FileNotFoundError means a parent component does not exist. This eliminates the 
race condition of having to test whether the parent path exists. In Windows, 
it's not possible to differentiate the two cases by error code alone, so 
unfortunately Path.mkdir() tries to create the parent path when it contains a 
non-directory component, and thus a nested FileExistsError exception is raised.

The design of os.makedirs() instead checks whether the parent exists before 
trying to create it, and ignores FileExitsError if creating the parent tree 
fails due to a race condition. This design behaves a bit more consistently 
across platforms, but it still fails with a different exception on Windows vs 
POSIX, i.e. FileNotFoundError vs NotADirectoryError.

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42872>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to