Support `exist_ok` for `pathlib`'s `Path.copy_into()`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
Background
The Path.copy_into() method (introduced in Python 3.14) is used to copy files or directories into another directory.
The behaviour of files being copied and directories being copied is inconsistent when the destination directory contains a file or directory with the same name.
Examples
When a file is copied into a directory that already has a file with that name, the copied file overwrites the file at the destination directory:
"""
# Copying a file into a directory that already has a file with the same name #
Directory Structure:
dir_a
|__my_file.txt
dir_b
|__my_file.txt
"""
from pathlib import Path
dir_a_my_file = Path("dir_a") / "my_file.txt"
dir_b = Path("dir_b")
# Act
dir_a_my_file.copy_into(dir_b) # Success - dir_a/my_file.txt overwrites dir_b/my_file.txt
However, when a directory is copied into another directory (one which contains a sub-directory with the same name),
the copy operation fails and FileExistsError is raised:
"""
# Copying a directory into a directory, that contains a sub-directory with the same name #
Directory Structure:
dir_a
|__my_dir
dir_b
|__my_dir
"""
from pathlib import Path
dir_a_my_dir = Path("dir_a") / "my_dir"
dir_b = Path("dir_b")
# Act
dir_a_my_dir.copy_into(dir_b) # Failure - FileExistsError is raised.
Problem
The behaviour of the copy_into() method in the file case (success) and in the directory case (failure) is inconsistent.
It is also inconsistent with how Linux (when using cp -r) handles this exact same case.
Expected Result
Instead of raising an exception, the copied directory should be merged with the existing one.
Fix Suggestion
-
Add a default
exist_ok=Trueparameter tocopy_into().
This parameter is already used in thePath.touch()andPath.mkdir()methods, and should be familiar to users. -
When a file is copied into a destination directory that already has a file with the same name:
-
If
exist_ok=True, the copied file should overwrite the file at the destination directory.
This is the already the current behaviour, so no additional changes are required. -
If
exist_ok=False, aFileExistsErrorexception will be raised. This is consistent with the behaviour ofPath.touch().
-
-
When a directory is copied into a destination directory that contains a sub-directory with the same name:
- If
exist_ok=True, the copied directory will be merged with the destination's sub-directory.
This is consistent with the behaviour of copying in Linux. - If
exist_ok=False, aFileExistsErrorexception will be raised. This is consistent with the behaviour ofPath.mkdir().
- If
Thank you.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-143058
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the pathlib implementation and tests for Path.copy_into(), then compare its current file and directory collision behavior with Path.touch() and Path.mkdir(). Done means exist_ok is supported, recursive directory copies merge or raise as specified, and both cases are covered by tests; note that linked PR gh-143058 already indicates work is underway.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100