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: Add copy() method to pathlib
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, seblin, serhiy.storchaka
Priority: normal Keywords:

Created on 2019-10-29 05:55 by seblin, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg355616 - (view) Author: Sebastian Linke (seblin) Date: 2019-10-29 05:55
pathlib.Path() could wrap shutil.copy() with something like this:

def copy(self, target):
    if not self.is_file():
        # No support for directories here
        raise ValueError("Path must point to a regular file")
    # copy() appends filename when Path is copied to a directory
    # see shutil.copy() docstring and source for details
    target = shutil.copy(self, target)
    return self.__class__(target)
msg355617 - (view) Author: Sebastian Linke (seblin) Date: 2019-10-29 06:04
Docstring could be:

Copy file content and permission bits of this path to the target path
and return a new Path instance pointing to the target path.
If target is a directory, the base filename of this path is added to
the target and a new file corresponding to the target is created.
If target points to an existing file, that file is overwritten.
msg355620 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-29 06:52
Since shutil.copy() accepts path-like object, I do not think there is a need to add the copy method to Path.

As for additional limitation, it looks arbitrary and application specific. You can easy implement a simple helper function as you just demonstrated. And you can easily modify it if your needs change (for example if you decide to not copy links). Not every three lines of code should be added as a function to the stdlib.
msg355671 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-10-29 19:07
And just to add Serhiy's logic, think of pathlib more as an equivalent to os.path than a be-all solution to anything path-related.
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82808
2019-10-29 19:07:13brett.cannonsetnosy: + brett.cannon
messages: + msg355671
2019-10-29 06:52:53serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg355620

resolution: rejected
stage: resolved
2019-10-29 06:04:06seblinsetmessages: + msg355617
2019-10-29 05:55:17seblincreate