@@ -1411,6 +1411,70 @@ Creating files and directories
1411
1411
available. In previous versions, :exc: `NotImplementedError ` was raised.
1412
1412
1413
1413
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
+
1414
1478
Other methods
1415
1479
^^^^^^^^^^^^^
1416
1480
@@ -1528,47 +1592,6 @@ Other methods
1528
1592
available. In previous versions, :exc: `NotImplementedError ` was raised.
1529
1593
1530
1594
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
-
1572
1595
.. method :: Path.absolute()
1573
1596
1574
1597
Make the path absolute, without normalization or resolving symlinks.
@@ -1611,25 +1634,6 @@ Other methods
1611
1634
strict mode, and no exception is raised in non-strict mode. In previous
1612
1635
versions, :exc: `RuntimeError ` is raised no matter the value of *strict *.
1613
1636
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
-
1633
1637
1634
1638
.. _pathlib-pattern-language :
1635
1639
0 commit comments