python / python/cpython

shutil's move function fails to handle files opened by another process

Ouverte
#120,882 0 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

OS-windows type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug report

Bug description:

Problem:

When attempting to move a file using shutil.move from one location to another, if the file is opened by another process (if file is opened in other python script using open function), shutil.move raises an error (WinError 32: The process cannot access the file because it is being used by another process). Despite this error, the file is still copied to the destination directory, which is unexpected behavior.

Details:

The shutil.move function is designed to handle moving files or directories between locations. However, it does not check if the file is currently open by another process before attempting to move it. This results in an error being raised, but the file is still copied to destination regardless of the error condition.

Solution:

To address this issue, I modified the shutil.move function to include an additional check for OSError with errno.EACCES (Permission Denied) in the exception handling block. This ensures that if the file cannot be moved due to being opened by another process, the operation is aborted cleanly without copying the file to maintain the integrity of the move function as well as file system.

Here is the modified part of the shutil.move function

    except OSError as exc:
        if exc.errno == errno.EACCES:
            raise  # Propagate the PermissionError
     # Existing code

Proposal:

I propose adding this error handling improvement to the shutil.move function in Python's standard library. This enhancement will make file operations more reliable in scenarios where files may be concurrently accessed by multiple processes

Steps to Reproduce:

  • Open a file (test.pdf) in any python script using open function in read mode.
  • Attempt to move the file using shutil.move to another directory.
  • Observe the WinError 32 exception and incorrect copying behavior.
  • Close the file (To avoid memory leakage).

NOTE: Avoid to use context manager here for opening file to find the error.
You can also run the following script to get the error.

import shutil

src = "C://Users//Desktop//Downloads//test.pdf" # Provide full file path of your system.
dst_dir = "D://Files" # Provide destination directory path.
f = open(src, "r")
try:
    shutil.move(src, dst_dir)
except Exception as e:
    print(e)
finally:
    f.close()

Expected Behavior:

When shutil.move encounters a situation where the source file is open in another process (WinError 32), it should raise an exception and abort the operation without performing any partial file copying to the destination.

Impact:

This issue affects users trying to use shutil.move to relocate files that are concurrently accessed by other processes. The proposed solution aims to improve the robustness and expected behavior of the function in such scenarios.

CPython versions tested on:

3.10

Operating systems tested on:

Windows

Linked PRs
  • gh-120883

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez au point d’entrée shutil.move et reproduisez le comportement Windows signalé avec un fichier source maintenu ouvert par un autre processus. Vérifiez que l’opération lève l’exception attendue sans laisser de fichier de destination copié, et examinez le PR lié gh-120883 avant de commencer, car du travail est déjà associé à cet issue.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
operating-systems
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.