This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: shutil should accept pathlib types
Type: behavior Stage: resolved
Components: Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mkesper, r.david.murray, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2015-01-14 13:02 by mkesper, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg234025 - (view) Author: Michael Kesper (mkesper) Date: 2015-01-14 13:02
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.
msg234027 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-01-14 13:42
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).
msg290508 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-26 06:31
Is this issue outdated since implementing a file system path protocol (PEP 519) in 3.6?
msg290582 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-27 10:57
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 ;-)
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67430
2017-03-27 10:57:17vstinnersetstatus: pending -> closed

nosy: + vstinner
messages: + msg290582

resolution: fixed
stage: resolved
2017-03-26 06:31:25serhiy.storchakasetstatus: open -> pending
nosy: + serhiy.storchaka
messages: + msg290508

2015-01-14 13:42:19r.david.murraysetnosy: + r.david.murray
messages: + msg234027
2015-01-14 13:02:56mkespercreate