Skip to content

Commit d88a1f2

Browse files
authored
GH-119054: Add "Renaming and deleting" section to pathlib docs. (#120465)
Add dedicated subsection for `pathlib.Path.rename()`, `replace()`, `unlink()` and `rmdir()`.
1 parent a3711af commit d88a1f2

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

Doc/library/pathlib.rst

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,70 @@ Creating files and directories
14291429
available. In previous versions, :exc:`NotImplementedError` was raised.
14301430

14311431

1432+
Renaming and deleting
1433+
^^^^^^^^^^^^^^^^^^^^^
1434+
1435+
.. method:: Path.rename(target)
1436+
1437+
Rename this file or directory to the given *target*, and return a new
1438+
:class:`!Path` instance pointing to *target*. On Unix, if *target* exists
1439+
and is a file, it will be replaced silently if the user has permission.
1440+
On Windows, if *target* exists, :exc:`FileExistsError` will be raised.
1441+
*target* can be either a string or another path object::
1442+
1443+
>>> p = Path('foo')
1444+
>>> p.open('w').write('some text')
1445+
9
1446+
>>> target = Path('bar')
1447+
>>> p.rename(target)
1448+
PosixPath('bar')
1449+
>>> target.open().read()
1450+
'some text'
1451+
1452+
The target path may be absolute or relative. Relative paths are interpreted
1453+
relative to the current working directory, *not* the directory of the
1454+
:class:`!Path` object.
1455+
1456+
It is implemented in terms of :func:`os.rename` and gives the same guarantees.
1457+
1458+
.. versionchanged:: 3.8
1459+
Added return value, return the new :class:`!Path` instance.
1460+
1461+
1462+
.. method:: Path.replace(target)
1463+
1464+
Rename this file or directory to the given *target*, and return a new
1465+
:class:`!Path` instance pointing to *target*. If *target* points to an
1466+
existing file or empty directory, it will be unconditionally replaced.
1467+
1468+
The target path may be absolute or relative. Relative paths are interpreted
1469+
relative to the current working directory, *not* the directory of the
1470+
:class:`!Path` object.
1471+
1472+
.. versionchanged:: 3.8
1473+
Added return value, return the new :class:`!Path` instance.
1474+
1475+
1476+
.. method:: Path.unlink(missing_ok=False)
1477+
1478+
Remove this file or symbolic link. If the path points to a directory,
1479+
use :func:`Path.rmdir` instead.
1480+
1481+
If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
1482+
raised if the path does not exist.
1483+
1484+
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1485+
ignored (same behavior as the POSIX ``rm -f`` command).
1486+
1487+
.. versionchanged:: 3.8
1488+
The *missing_ok* parameter was added.
1489+
1490+
1491+
.. method:: Path.rmdir()
1492+
1493+
Remove this directory. The directory must be empty.
1494+
1495+
14321496
Other methods
14331497
^^^^^^^^^^^^^
14341498

@@ -1545,47 +1609,6 @@ Other methods
15451609
available. In previous versions, :exc:`NotImplementedError` was raised.
15461610

15471611

1548-
.. method:: Path.rename(target)
1549-
1550-
Rename this file or directory to the given *target*, and return a new Path
1551-
instance pointing to *target*. On Unix, if *target* exists and is a file,
1552-
it will be replaced silently if the user has permission.
1553-
On Windows, if *target* exists, :exc:`FileExistsError` will be raised.
1554-
*target* can be either a string or another path object::
1555-
1556-
>>> p = Path('foo')
1557-
>>> p.open('w').write('some text')
1558-
9
1559-
>>> target = Path('bar')
1560-
>>> p.rename(target)
1561-
PosixPath('bar')
1562-
>>> target.open().read()
1563-
'some text'
1564-
1565-
The target path may be absolute or relative. Relative paths are interpreted
1566-
relative to the current working directory, *not* the directory of the Path
1567-
object.
1568-
1569-
It is implemented in terms of :func:`os.rename` and gives the same guarantees.
1570-
1571-
.. versionchanged:: 3.8
1572-
Added return value, return the new Path instance.
1573-
1574-
1575-
.. method:: Path.replace(target)
1576-
1577-
Rename this file or directory to the given *target*, and return a new Path
1578-
instance pointing to *target*. If *target* points to an existing file or
1579-
empty directory, it will be unconditionally replaced.
1580-
1581-
The target path may be absolute or relative. Relative paths are interpreted
1582-
relative to the current working directory, *not* the directory of the Path
1583-
object.
1584-
1585-
.. versionchanged:: 3.8
1586-
Added return value, return the new Path instance.
1587-
1588-
15891612
.. method:: Path.absolute()
15901613

15911614
Make the path absolute, without normalization or resolving symlinks.
@@ -1628,25 +1651,6 @@ Other methods
16281651
strict mode, and no exception is raised in non-strict mode. In previous
16291652
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.
16301653

1631-
.. method:: Path.rmdir()
1632-
1633-
Remove this directory. The directory must be empty.
1634-
1635-
1636-
.. method:: Path.unlink(missing_ok=False)
1637-
1638-
Remove this file or symbolic link. If the path points to a directory,
1639-
use :func:`Path.rmdir` instead.
1640-
1641-
If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
1642-
raised if the path does not exist.
1643-
1644-
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1645-
ignored (same behavior as the POSIX ``rm -f`` command).
1646-
1647-
.. versionchanged:: 3.8
1648-
The *missing_ok* parameter was added.
1649-
16501654

16511655
.. _pathlib-pattern-language:
16521656

0 commit comments

Comments
 (0)