-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Docs: the difference between Path.rename() and Path.replace() is not obvious #72073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi. On reading the doc for pathlib I've stuck with What's the difference? Going to pathlib's source code I've figured out that methods are use different functions from But the documentation for Could anybody explain the difference? Also, at least the doc for |
The existing docs are pretty clear on the difference: rename is only guaranteed to replace an existing file on unix (which I think means posix in this context), whereas replace always replaces the file, regardless of platform. I'm actually surprised that rename is even part of the pathlib API, since its cross platform behavior is not consistent. It also seems as though the replace docs are incorrect, since they imply directories are replaced, but the os docs say they are not. The "this is a posix requirement" note in os.replace also seems imprecise. Windows is not posix, so that note leaves me wondering if replace is atomic on Windows or not. That should be clarified one way or another. I think adding the cross platform note in the pathlib docs is reasonable, since it addresses the question of why there are two similar functions. |
Just keeping the reference to Aha, Sorry, I've missed this. Maybe it worth to add `.. warning:` section for both `os.rename` and `pathlib.Path.rename`? |
No, we don't like to overuse warnings. |
The pathlib documentation of Path.rename() says "[o]n Unix, if target exists and is a file, it will be replaced silently if the user has permission". This leaves the behavior on Windows in question. The reader has to scroll down to the correspondence table to link to the documentation of os.rename() and presume that Path.rename() also raises FileExistsError for this case. The Path.rename() docs should just state upfront that "[o]n Windows, if target exists, FileExistsError will be raised". |
I just wanted to note that I find the current documentation confusing. I had to search and find this issue report to understand the difference. As mentioned above the effect of Path.rename on non-Unix systems is ambiguous in the current documentation, so it is not clear how replace differs from rename. |
pythonGH-72073 https://docs.python.org/3.12/library/pathlib.htmlGH-pathlib.Path.rename (cherry picked from commit f51ed04) Co-authored-by: Stanley <[email protected]>
pythonGH-72073 https://docs.python.org/3.12/library/pathlib.htmlGH-pathlib.Path.rename (cherry picked from commit f51ed04) Co-authored-by: Stanley <[email protected]>
) GH-72073 https://docs.python.org/3.12/library/pathlib.htmlGH-pathlib.Path.rename (cherry picked from commit f51ed04) Co-authored-by: Stanley <[email protected]> Automerge-Triggered-By: GH:brettcannon
) GH-72073 https://docs.python.org/3.12/library/pathlib.htmlGH-pathlib.Path.rename (cherry picked from commit f51ed04) Co-authored-by: Stanley <[email protected]> Automerge-Triggered-By: GH:brettcannon
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: