Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5ad3b7

Browse files
[3.13] GH-119054: Add "Renaming and deleting" section to pathlib docs. (GH-120465) (#120472)
GH-119054: Add "Renaming and deleting" section to pathlib docs. (GH-120465) Add dedicated subsection for `pathlib.Path.rename()`, `replace()`, `unlink()` and `rmdir()`. (cherry picked from commit d88a1f2) Co-authored-by: Barney Gale <[email protected]>
1 parent 15c3d00 commit d5ad3b7

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
@@ -1411,6 +1411,70 @@ Creating files and directories
14111411
available. In previous versions, :exc:`NotImplementedError` was raised.
14121412

14131413

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

@@ -1528,47 +1592,6 @@ Other methods
15281592
available. In previous versions, :exc:`NotImplementedError` was raised.
15291593

15301594

1531-
.. method:: Path.rename(target)
1532-
1533-
Rename this file or directory to the given *target*, and return a new Path
1534-
instance pointing to *target*. On Unix, if *target* exists and is a file,
1535-
it will be replaced silently if the user has permission.
1536-
On Windows, if *target* exists, :exc:`FileExistsError` will be raised.
1537-
*target* can be either a string or another path object::
1538-
1539-
>>> p = Path('foo')
1540-
>>> p.open('w').write('some text')
1541-
9
1542-
>>> target = Path('bar')
1543-
>>> p.rename(target)
1544-
PosixPath('bar')
1545-
>>> target.open().read()
1546-
'some text'
1547-
1548-
The target path may be absolute or relative. Relative paths are interpreted
1549-
relative to the current working directory, *not* the directory of the Path
1550-
object.
1551-
1552-
It is implemented in terms of :func:`os.rename` and gives the same guarantees.
1553-
1554-
.. versionchanged:: 3.8
1555-
Added return value, return the new Path instance.
1556-
1557-
1558-
.. method:: Path.replace(target)
1559-
1560-
Rename this file or directory to the given *target*, and return a new Path
1561-
instance pointing to *target*. If *target* points to an existing file or
1562-
empty directory, it will be unconditionally replaced.
1563-
1564-
The target path may be absolute or relative. Relative paths are interpreted
1565-
relative to the current working directory, *not* the directory of the Path
1566-
object.
1567-
1568-
.. versionchanged:: 3.8
1569-
Added return value, return the new Path instance.
1570-
1571-
15721595
.. method:: Path.absolute()
15731596

15741597
Make the path absolute, without normalization or resolving symlinks.
@@ -1611,25 +1634,6 @@ Other methods
16111634
strict mode, and no exception is raised in non-strict mode. In previous
16121635
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.
16131636

1614-
.. method:: Path.rmdir()
1615-
1616-
Remove this directory. The directory must be empty.
1617-
1618-
1619-
.. method:: Path.unlink(missing_ok=False)
1620-
1621-
Remove this file or symbolic link. If the path points to a directory,
1622-
use :func:`Path.rmdir` instead.
1623-
1624-
If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
1625-
raised if the path does not exist.
1626-
1627-
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1628-
ignored (same behavior as the POSIX ``rm -f`` command).
1629-
1630-
.. versionchanged:: 3.8
1631-
The *missing_ok* parameter was added.
1632-
16331637

16341638
.. _pathlib-pattern-language:
16351639

0 commit comments

Comments
 (0)