Issue 34069: shutil.move fails with AttributeError (original) (raw)

from shutil import move from pathlib import Path

a = Path("s") b = Path("a.txt")

move(b, a)

This will throw AttributeError: 'WindowsPath' object has no attribute 'rstrip'

From the document, it should able to move:

If the destination is an existing directory, then src is moved inside that directory. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.

If a = Path("s/a.txt"), it does not throw error.

Enviroment: Window 10 Python 3.7.0

This issue isn't specific to Windows. It's a bug in shutil._basename:

def _basename(path):
    # A basename() variant which first strips the trailing slash, if present.
    # Thus we always get the last component of the path, even for directories.
    sep = os.path.sep + (os.path.altsep or '')
    return os.path.basename(path.rstrip(sep))

It should use os.fspath(path) to get the path as a string.