python / python/cpython

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

Aperta
#143,052 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

pending stdlib topic-pathlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia individuando l'implementazione di pathlib e i test per Path.copy_into(), quindi confronta il comportamento attuale in caso di collisione tra file e directory con quello di Path.touch() e Path.mkdir(). Il lavoro è completato quando exist_ok è supportato, le copie ricorsive delle directory vengono unite o sollevano un'eccezione come specificato ed entrambi i casi sono coperti dai test; nota che la PR collegata gh-143058 indica già che il lavoro è in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
operating-systems
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.