Skip to content

Commit 2391cd0

Browse files
gh-72073: Clarify pathlib rename() and replace() docs
1 parent 836fbda commit 2391cd0

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

Doc/library/pathlib.rst

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,9 +1626,14 @@ Copying, moving and deleting
16261626
.. method:: Path.rename(target)
16271627

16281628
Rename this file or directory to the given *target*, and return a new
1629-
:class:`!Path` instance pointing to *target*. On Unix, if *target* exists
1630-
and is a file, it will be replaced silently if the user has permission.
1631-
On Windows, if *target* exists, :exc:`FileExistsError` will be raised.
1629+
:class:`!Path` instance pointing to *target*. This method is implemented
1630+
in terms of :func:`os.rename`, and gives the same guarantees.
1631+
1632+
On Unix, if *target* exists and is a file, it will be replaced silently if
1633+
the user has permission. On Windows, if *target* exists,
1634+
:exc:`FileExistsError` will be raised. If you want to replace the target
1635+
consistently across platforms, use :meth:`Path.replace`.
1636+
16321637
*target* can be either a string or another path object::
16331638

16341639
>>> p = Path('foo')
@@ -1644,17 +1649,20 @@ Copying, moving and deleting
16441649
relative to the current working directory, *not* the directory of the
16451650
:class:`!Path` object.
16461651

1647-
It is implemented in terms of :func:`os.rename` and gives the same guarantees.
1648-
16491652
.. versionchanged:: 3.8
16501653
Added return value, return the new :class:`!Path` instance.
16511654

16521655

16531656
.. method:: Path.replace(target)
16541657

16551658
Rename this file or directory to the given *target*, and return a new
1656-
:class:`!Path` instance pointing to *target*. If *target* points to an
1657-
existing file or empty directory, it will be unconditionally replaced.
1659+
:class:`!Path` instance pointing to *target*. Unlike :meth:`Path.rename`,
1660+
if *target* points to an existing file or empty directory, it will be
1661+
unconditionally replaced.
1662+
1663+
This method is implemented in terms of :func:`os.replace`; use it when you
1664+
want consistent cross-platform replacement semantics for an existing file or
1665+
empty directory.
16581666

16591667
The target path may be absolute or relative. Relative paths are interpreted
16601668
relative to the current working directory, *not* the directory of the

0 commit comments

Comments
 (0)