Skip to content

shutil.move fails with AttributeError #78250

Closed

Description

@joshuaavalon
BPO 34069
Nosy @pfmoore, @tjguk, @berkerpeksag, @zware, @eryksun, @zooba
Superseder
  • bpo-32689: shutil.move raises AttributeError if first argument is a pathlib.Path object and destination is a directory
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-07-08.12:12:32.399>
    created_at = <Date 2018-07-08.10:57:23.096>
    labels = ['3.7', '3.8', 'type-bug', 'library', 'easy']
    title = 'shutil.move fails with AttributeError'
    updated_at = <Date 2018-07-08.12:12:32.397>
    user = 'https://bugs.python.org/joshuaavalon'

    bugs.python.org fields:

    activity = <Date 2018-07-08.12:12:32.397>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-07-08.12:12:32.399>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2018-07-08.10:57:23.096>
    creator = 'joshuaavalon'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34069
    keywords = ['easy']
    message_count = 3.0
    messages = ['321270', '321272', '321274']
    nosy_count = 7.0
    nosy_names = ['paul.moore', 'tim.golden', 'berker.peksag', 'zach.ware', 'eryksun', 'steve.dower', 'joshuaavalon']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '32689'
    type = 'behavior'
    url = 'https://bugs.python.org/issue34069'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    Activity

    1. joshuaavalon commented on Jul 8, 2018

      joshuaavalonmannequin
      MannequinAuthor
      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

    2. eryksun commented on Jul 8, 2018

      @eryksun
      Contributor

      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.

    3. added
      stdlibStandard Library Python modules in the Lib/ directory
      and removed on Jul 8, 2018
    4. changed the title [-]Fail to move file in Windows (AttributeError)[/-] [+]shutil.move fails with AttributeError[/+] on Jul 8, 2018
    5. added
      type-bugAn unexpected behavior, bug, or error
      on Jul 8, 2018
    6. berkerpeksag commented on Jul 8, 2018

      @berkerpeksag
      Member

      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.

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of life3.8 (EOL)end of lifeeasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions