[issue42929] On Windows, shutil.move doesn't raise FileExistsError if dst exists like os.rename

Eryk Sun report at bugs.python.org
Fri Jan 15 11:18:14 EST 2021


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

I can help, but in this case there isn't much to do. Just replace os.rename() with os.replace(), make a minor doc change, and maybe add a test that ensures a junction can be moved over an existing file on the same filesystem. For example:

    >>> os.mkdir('temp')
    >>> _winapi.CreateJunction('temp', 'src')
    >>> os.lstat('src').st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT
    True
    >>> open('dst', 'w').close()

The current implementation tries copytree() on the junction mountpoint and fails to create a new directory named "dst":

    >>> try: shutil.move('src', 'dst')
    ... except FileExistsError as e: e
    ...
    FileExistsError(17, 'Cannot create a file when that file already exists')

But move() should simply replace "dst" with the junction via os.replace():

    >>> os.replace('src', 'dst')
    >>> os.lstat('dst').st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT
    True

----------

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


More information about the Python-bugs-list mailing list