python / python/cpython

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

オープン
#143,052 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。