[issue43219] shutil.copy raises IsADirectoryError when the directory does not actually exist

Eryk Sun report at bugs.python.org
Sun Feb 14 01:49:04 EST 2021


Eryk Sun <eryksun at gmail.com> added the comment:

> IsADirectoryError: [Errno 21] Is a directory: 'not_a_dir/'

The trailing slash forces the OS to handle "not_a_dir" as a directory [1]. 

    A pathname that contains at least one non- <slash> character and that 
    ends with one or more trailing <slash> characters shall not be resolved
    successfully unless the last pathname component before the trailing 
    <slash> characters names an existing directory or a directory entry 
    that is to be created for a directory immediately after the pathname is
    resolved. 

Mode "w" corresponds to low-level POSIX open() flags O_CREAT | O_TRUNC | O_WRONLY. If write access is requested for a directory, the open() system call must fail with EISDIR [2].

    [EISDIR]
        The named file is a directory and oflag includes O_WRONLY or O_RDWR,
        or includes O_CREAT without O_DIRECTORY.

In most cases, opening a directory with O_CREAT also fails with E_ISDIR. POSIX does permit an implementation to create a directory with O_CREAT | O_DIRECTORY. In Linux, however, O_CREAT always creates a regular file, regardless of O_DIRECTORY, so open(pathname, O_CREAT | flags) always fails with EISDIR when pathname is an existing directory or names a directory by way of a trailing slash.

---
[1] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
[2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html

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

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


More information about the Python-bugs-list mailing list