shutil's move function fails to handle files opened by another process
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 36k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
openfunction 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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne am Einstiegspunkt shutil.move und reproduziere das gemeldete Windows-Verhalten mit einer Quelldatei, die von einem anderen Prozess geöffnet gehalten wird. Überprüfe, dass der Vorgang die erwartete Ausnahme auslöst, ohne eine kopierte Zieldatei zu hinterlassen, und sieh dir den verknüpften PR gh-120883 an, bevor du beginnst, da diesem Issue bereits Arbeit zugeordnet ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- operating-systems
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100