Skip to content

GH-73991: Add pathlib.Path.copy() - #119058

Merged
barneygale merged 19 commits into
python:mainfrom
barneygale:gh-73991-copy
Jun 14, 2024
Merged

GH-73991: Add pathlib.Path.copy()#119058
barneygale merged 19 commits into
python:mainfrom
barneygale:gh-73991-copy

Conversation

@barneygale

@barneygalebarneygale commented May 14, 2024

Copy link
Copy Markdown
Contributor

Add a Path.copy() method that copies the content of one file to another.

This method is similar to shutil.copyfile() but differs in the following ways:

The method is deliberately less specified than the shutil functions to allow OS-specific optimizations that might copy more or less metadata.

Incorporates code by @illia-v and @giampaolo from #81338 and #93152 respectively.


📚 Documentation preview 📚: https://cpython-previews--119058.org.readthedocs.build/

Add a `Path.copy()` method that copies a file to a target file or directory
using `shutil.copy2()`.
In the private pathlib ABCs, we add a version that supports copying from
one instance of `PathBase` to another. We don't copy metadata, because
doing so probably requires new APIs that haven't been designed yet.
@barneygalebarneygale changed the title GH-73991: Add pathlib.Path.copy() method.GH-73991: Add pathlib.Path.copy()May 15, 2024
@barneygale

Copy link
Copy Markdown
ContributorAuthor

Marking this PR as a draft, because I think we'll need to introduce os.copy() first.

@barneygale
barneygale marked this pull request as draft May 27, 2024 14:40
@barneygale
barneygale marked this pull request as ready for review June 3, 2024 06:42
@barneygale
barneygale requested a review from zoobaJune 3, 2024 20:56

@zoobazooba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some thoughts, nothing I'd necessarily block the PR on. I like the way it looks overall!

Comment threadDoc/library/pathlib.rst Outdated
Comment threadLib/pathlib/_local.py Outdated
Comment threadLib/pathlib/_abc.py Outdated
Comment threadLib/pathlib/_local.py Outdated
Comment threadLib/pathlib/_os.py Outdated
Comment threadLib/pathlib/_os.py Outdated
Comment threadLib/pathlib/_os.py Outdated
Comment threadModules/_winapi.c Outdated
barneygaleand others added 2 commits June 6, 2024 20:21
@barneygale
barneygale requested review from eryksun and zoobaJune 12, 2024 22:07
@zooba

Copy link
Copy Markdown
Member

Looks fine to me, but I only quickly reskimmed it. I'll be away for the next few days, so don't wait for me if you guys think it's ready.

@barneygale
barneygale merged commit 7c38097 into python:mainJun 14, 2024
mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
Add a `Path.copy()` method that copies the content of one file to another.
This method is similar to `shutil.copyfile()` but differs in the following ways:
- Uses `fcntl.FICLONE` where available (see pythonGH-81338)
- Uses `os.copy_file_range` where available (see pythonGH-81340)
- Uses `_winapi.CopyFile2` where available, even though this copies more metadata than the other implementations. This makes `WindowsPath.copy()` more similar to `shutil.copy2()`.
The method is presently _less_ specified than the `shutil` functions to allow OS-specific optimizations that might copy more or less metadata.
Incorporates code from pythonGH-81338 and pythonGH-93152.
Co-authored-by: Eryk Sun <eryksun@gmail.com>
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
Add a `Path.copy()` method that copies the content of one file to another.
This method is similar to `shutil.copyfile()` but differs in the following ways:
- Uses `fcntl.FICLONE` where available (see pythonGH-81338)
- Uses `os.copy_file_range` where available (see pythonGH-81340)
- Uses `_winapi.CopyFile2` where available, even though this copies more metadata than the other implementations. This makes `WindowsPath.copy()` more similar to `shutil.copy2()`.
The method is presently _less_ specified than the `shutil` functions to allow OS-specific optimizations that might copy more or less metadata.
Incorporates code from pythonGH-81338 and pythonGH-93152.
Co-authored-by: Eryk Sun <eryksun@gmail.com>
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Add a `Path.copy()` method that copies the content of one file to another.
This method is similar to `shutil.copyfile()` but differs in the following ways:
- Uses `fcntl.FICLONE` where available (see pythonGH-81338)
- Uses `os.copy_file_range` where available (see pythonGH-81340)
- Uses `_winapi.CopyFile2` where available, even though this copies more metadata than the other implementations. This makes `WindowsPath.copy()` more similar to `shutil.copy2()`.
The method is presently _less_ specified than the `shutil` functions to allow OS-specific optimizations that might copy more or less metadata.
Incorporates code from pythonGH-81338 and pythonGH-93152.
Co-authored-by: Eryk Sun <eryksun@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@barneygale@zooba@eryksun