python / python/cpython

Support `exist_ok` for `pathlib`'s `Path.copy_into()`

未关闭
#143,052 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

pending stdlib topic-pathlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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
  1. Add a default exist_ok=True parameter to copy_into().
    This parameter is already used in the Path.touch() and Path.mkdir() methods, and should be familiar to users.

  2. 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, a FileExistsError exception will be raised. This is consistent with the behaviour of Path.touch().

  3. 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, a FileExistsError exception will be raised. This is consistent with the behaviour of Path.mkdir().

Thank you.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-143058

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先定位 pathlib 的实现以及 Path.copy_into() 的测试,然后将其当前的文件和目录冲突行为与 Path.touch() 和 Path.mkdir() 进行比较。完成的标准是支持 exist_ok,递归目录复制按指定方式合并或引发异常,并且这两种情况都有测试覆盖;注意,已关联的 PR gh-143058 已表明相关工作正在进行中。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。