python / python/cpython

Confusing error message when rename target is an open file

Open
#129,230 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-pathlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Background

Writing a tool to update and rename some files, I made a mistake and tried to rename a file while it was still open. This works on Linux, but raises an error when writing to a Windows network shares. This is known and documented - see Path.rename docs.

The issue

The problem I have is that the error message is incorrect. It says "FileNotFound", which is confusing because the files do exist. If I had got a more useful message, I would have worked out what the problem was with my tool much sooner.

Repro case

    file1 = Path("test1.txt")
    file2 = Path("test2.txt")
    file1.open("w").write("file 1 text\n")
    file2.open("w").write("file 2 text\n")
    with file1.open():
        file2.rename(file1)

Outcomes;
On linux, writing to pwd or a linux network share, file is rewritten without error.
On linux, writing to a Windows network share, an incorrect error message is given.

$ python test.py
Traceback (most recent call last):
  File "/home/jontwo/test.py", line 17, in <module>
    main()
  File "/home/jontwo/test.py", line 13, in main
    file2.rename(file1)
  File "/usr/lib/python3.12/pathlib.py", line 1363, in rename
    os.rename(self, target)
FileNotFoundError: [Errno 2] No such file or directory: 'test2.txt' -> 'test1.txt'

On Windows, writing to pwd, a useful error message is given.

c:\temp>python test.py
Traceback (most recent call last):
  File "c:\temp\test.py", line 16, in <module>
    main()
  File "c:\temp\test.py", line 12, in main
    file2.rename(file1)
  File "C:\OSGeo4W\apps\Python312\Lib\pathlib.py", line 1363, in rename
    os.rename(self, target)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'test2.txt' -> 'test1.txt'
CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the pathlib.Path.rename case shown in the issue on Linux and Windows network-share conditions. Inspect pathlib.py around the reported rename entry point and compare the resulting errors with the documented Path.rename behavior. Done means the open-target case no longer presents a misleading FileNotFound message, with the platform-specific behavior covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.