classification
Title: shutil.move fails with AttributeError
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: shutil.move raises AttributeError if first argument is a pathlib.Path object and destination is a directory
View: 32689
Assigned To: Nosy List: berker.peksag, eryksun, joshuaavalon, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: easy

Created on 2018-07-08 10:57 by joshuaavalon, last changed 2018-07-08 12:12 by berker.peksag. This issue is now closed.

Messages (3)
msg321270 - (view) Author: Joshua Avalon (joshuaavalon) Date: 2018-07-08 10:57
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
msg321272 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2018-07-08 11:33
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.
msg321274 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-07-08 12:12
This is a duplicate of bpo-32689.

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

PR 5393 already does that.
History
Date User Action Args
2018-07-08 12:12:32berker.peksagsetstatus: open -> closed

superseder: shutil.move raises AttributeError if first argument is a pathlib.Path object and destination is a directory

nosy: + berker.peksag
messages: + msg321274
resolution: duplicate
stage: needs patch -> resolved
2018-07-08 11:33:27eryksunsettype: behavior
title: Fail to move file in Windows (AttributeError) -> shutil.move fails with AttributeError
components: + Library (Lib), - Windows

keywords: + easy
nosy: + eryksun
versions: + Python 3.6, Python 3.8
messages: + msg321272
stage: needs patch
2018-07-08 10:57:23joshuaavaloncreate