Skip to content

Commit c9fc099

Browse files
[3.12] GH-119054: Add "Renaming and deleting" section to pathlib docs. (GH-120465) (#120473)
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 7719eef commit c9fc099

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

Doc/library/pathlib.rst

+64-60
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,70 @@ Creating files and directories
13161316
.. versionadded:: 3.10
13171317

13181318

1319+
Renaming and deleting
1320+
^^^^^^^^^^^^^^^^^^^^^
1321+
1322+
.. method:: Path.rename(target)
1323+
1324+
Rename this file or directory to the given *target*, and return a new
1325+
:class:`!Path` instance pointing to *target*. On Unix, if *target* exists
1326+
and is a file, it will be replaced silently if the user has permission.
1327+
On Windows, if *target* exists, :exc:`FileExistsError` will be raised.
1328+
*target* can be either a string or another path object::
1329+
1330+
>>> p = Path('foo')
1331+
>>> p.open('w').write('some text')
1332+
9
1333+
>>> target = Path('bar')
1334+
>>> p.rename(target)
1335+
PosixPath('bar')
1336+
>>> target.open().read()
1337+
'some text'
1338+
1339+
The target path may be absolute or relative. Relative paths are interpreted
1340+
relative to the current working directory, *not* the directory of the
1341+
:class:`!Path` object.
1342+
1343+
It is implemented in terms of :func:`os.rename` and gives the same guarantees.
1344+
1345+
.. versionchanged:: 3.8
1346+
Added return value, return the new :class:`!Path` instance.
1347+
1348+
1349+
.. method:: Path.replace(target)
1350+
1351+
Rename this file or directory to the given *target*, and return a new
1352+
:class:`!Path` instance pointing to *target*. If *target* points to an
1353+
existing file or empty directory, it will be unconditionally replaced.
1354+
1355+
The target path may be absolute or relative. Relative paths are interpreted
1356+
relative to the current working directory, *not* the directory of the
1357+
:class:`!Path` object.
1358+
1359+
.. versionchanged:: 3.8
1360+
Added return value, return the new :class:`!Path` instance.
1361+
1362+
1363+
.. method:: Path.unlink(missing_ok=False)
1364+
1365+
Remove this file or symbolic link. If the path points to a directory,
1366+
use :func:`Path.rmdir` instead.
1367+
1368+
If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
1369+
raised if the path does not exist.
1370+
1371+
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1372+
ignored (same behavior as the POSIX ``rm -f`` command).
1373+
1374+
.. versionchanged:: 3.8
1375+
The *missing_ok* parameter was added.
1376+
1377+
1378+
.. method:: Path.rmdir()
1379+
1380+
Remove this directory. The directory must be empty.
1381+
1382+
13191383
Other methods
13201384
^^^^^^^^^^^^^
13211385

@@ -1409,47 +1473,6 @@ Other methods
14091473
.. versionadded:: 3.9
14101474

14111475

1412-
.. method:: Path.rename(target)
1413-
1414-
Rename this file or directory to the given *target*, and return a new Path
1415-
instance pointing to *target*. On Unix, if *target* exists and is a file,
1416-
it will be replaced silently if the user has permission.
1417-
On Windows, if *target* exists, :exc:`FileExistsError` will be raised.
1418-
*target* can be either a string or another path object::
1419-
1420-
>>> p = Path('foo')
1421-
>>> p.open('w').write('some text')
1422-
9
1423-
>>> target = Path('bar')
1424-
>>> p.rename(target)
1425-
PosixPath('bar')
1426-
>>> target.open().read()
1427-
'some text'
1428-
1429-
The target path may be absolute or relative. Relative paths are interpreted
1430-
relative to the current working directory, *not* the directory of the Path
1431-
object.
1432-
1433-
It is implemented in terms of :func:`os.rename` and gives the same guarantees.
1434-
1435-
.. versionchanged:: 3.8
1436-
Added return value, return the new Path instance.
1437-
1438-
1439-
.. method:: Path.replace(target)
1440-
1441-
Rename this file or directory to the given *target*, and return a new Path
1442-
instance pointing to *target*. If *target* points to an existing file or
1443-
empty directory, it will be unconditionally replaced.
1444-
1445-
The target path may be absolute or relative. Relative paths are interpreted
1446-
relative to the current working directory, *not* the directory of the Path
1447-
object.
1448-
1449-
.. versionchanged:: 3.8
1450-
Added return value, return the new Path instance.
1451-
1452-
14531476
.. method:: Path.absolute()
14541477

14551478
Make the path absolute, without normalization or resolving symlinks.
@@ -1489,25 +1512,6 @@ Other methods
14891512
The *strict* parameter was added (pre-3.6 behavior is strict).
14901513

14911514

1492-
.. method:: Path.rmdir()
1493-
1494-
Remove this directory. The directory must be empty.
1495-
1496-
1497-
.. method:: Path.unlink(missing_ok=False)
1498-
1499-
Remove this file or symbolic link. If the path points to a directory,
1500-
use :func:`Path.rmdir` instead.
1501-
1502-
If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
1503-
raised if the path does not exist.
1504-
1505-
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1506-
ignored (same behavior as the POSIX ``rm -f`` command).
1507-
1508-
.. versionchanged:: 3.8
1509-
The *missing_ok* parameter was added.
1510-
15111515

15121516
Correspondence to tools in the :mod:`os` module
15131517
-----------------------------------------------

0 commit comments

Comments
 (0)