classification
Title: pathlib Path.replace raises OSError when target exists
Type: behavior Stage:
Components: Documentation Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, pitrou, selik, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2020-04-14 03:36 by selik, last changed 2020-04-14 04:45 by selik.

Messages (3)
msg366363 - (view) Author: Michael Selik (selik) * Date: 2020-04-14 03:36
The pathlib module ``Path.replace(target)`` states that "If target points to an existing file or directory, it will be unconditionally replaced."  However, this does not appear to be true.  I experience an OSError ``[Errno 66] Directory not empty`` when attempting to ``replace`` to an existant target.

https://docs.python.org/3/library/pathlib.html#pathlib.Path.replace


I see that others on StackOverflow encounter the same issue.  The top answer ignores the Python documentation and recommends removing the target directory before replacing.

https://stackoverflow.com/questions/50355180/use-pathlib-to-destructively-rename-one-directory-to-another-existing-directory
msg366364 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-04-14 04:10
replace under the hood uses os.replace. The docs for os.replace indicate error for certain scenarios where target is a directory : https://docs.python.org/3/library/os.html#os.replace . See also some difference between os.rename and os.replace : https://bugs.python.org/issue27886
msg366365 - (view) Author: Michael Selik (selik) * Date: 2020-04-14 04:45
The docs for ``os.replace`` says "If dst is a directory, OSError will be raised."  That's helpful, but the docs for ``pathlib.Path.replace`` make it seem like the target will be unconditionally replaced regardless of whether it's a file or directory.  Sure, there's the association of the two functions in the correspondence table in the pathlib docs, but that's easy to overlook when looking up ``pathlib.Path.replace``.

If nothing else, I guess this is a request to alter the docstring.
History
Date User Action Args
2020-04-14 04:45:59seliksetmessages: + msg366365
2020-04-14 04:10:34xtreaksetnosy: + docs@python, serhiy.storchaka, xtreak, pitrou
messages: + msg366364

assignee: docs@python
components: + Documentation
2020-04-14 03:36:59selikcreate