Skip to content

shutil should accept pathlib types #67430

Closed

Description

@mkesper
mannequin
BPO 23241
Nosy @vstinner, @bitdancer, @serhiy-storchaka

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 2017-03-27.10:57:17.056>
created_at = <Date 2015-01-14.13:02:56.723>
labels = ['type-bug']
title = 'shutil should accept pathlib types'
updated_at = <Date 2017-03-27.10:57:17.054>
user = 'https://bugs.python.org/mkesper'

bugs.python.org fields:

activity = <Date 2017-03-27.10:57:17.054>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2017-03-27.10:57:17.056>
closer = 'vstinner'
components = []
creation = <Date 2015-01-14.13:02:56.723>
creator = 'mkesper'
dependencies = []
files = []
hgrepos = []
issue_num = 23241
keywords = []
message_count = 4.0
messages = ['234025', '234027', '290508', '290582']
nosy_count = 4.0
nosy_names = ['vstinner', 'r.david.murray', 'mkesper', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue23241'
versions = ['Python 3.4']

Activity

  1. mkesper commented on Jan 14, 2015

    mkespermannequin
    MannequinAuthor
    source_path = Path('../data')
    destination_path = Path('//file_server/work/whatever')
    for file_name in source_path.glob('xyz-*'):
        shutil.copyfile(source_path / file_name, destination_path / file_name)

    leads to:

    Traceback (most recent call last):
      File "copy_shares_2_work.py", line 9, in <module>
        shutil.copyfile(source_path / file_name, destination_path / file_name)
      File "C:\Python34\lib\shutil.py", line 90, in copyfile
        if _samefile(src, dst):
      File "C:\Python34\lib\shutil.py", line 75, in _samefile
        return os.path.samefile(src, dst)
      File "C:\Python34\lib\genericpath.py", line 90, in samefile
        s1 = os.stat(f1)
    TypeError: argument should be string, bytes or integer, not WindowsPath

    Converting to str() works but is unexpected.

  2. bitdancer commented on Jan 14, 2015

    @bitdancer
    Member

    This is a specific example of a global discussion about handling of paths in the stdlib. So far we have elected not to provide special handling. The correct thing to do is call str. I'm not sure if shutil is special enough to be a special case, but it would need to be discussed in the context of the larger issue in some forum other than the bug tracker (probably python-dev).

  3. serhiy-storchaka commented on Mar 26, 2017

    @serhiy-storchaka
    Member

    Is this issue outdated since implementing a file system path protocol (PEP-519) in 3.6?

  4. vstinner commented on Mar 27, 2017

    @vstinner
    Member

    I'm unable to reproduce the bug on Python 3.6:
    ---

    from pathlib import Path
    import shutil
    import os
    def touch(name):
        with open(name, "x"):
            pass
    os.mkdir('a')
    touch('a/x')
    touch('a/y')
    touch('a/z')
    os.mkdir('b')
    source_path = Path('a')
    destination_path = Path('b')
    for file_name in source_path.glob('*'):
        file_name = file_name.name
        src = source_path / file_name
        dst = destination_path / file_name
        print("%r -> %r" % (src, dst))
        shutil.copyfile(src, dst)

    Output:
    ---
    PosixPath('a/y') -> PosixPath('b/y')
    PosixPath('a/x') -> PosixPath('b/x')
    PosixPath('a/z') -> PosixPath('b/z')
    ---

    Thank you PEP-519 ;-)

  5. 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

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions