@@ -1316,6 +1316,70 @@ Creating files and directories
1316
1316
.. versionadded :: 3.10
1317
1317
1318
1318
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
+
1319
1383
Other methods
1320
1384
^^^^^^^^^^^^^
1321
1385
@@ -1409,47 +1473,6 @@ Other methods
1409
1473
.. versionadded :: 3.9
1410
1474
1411
1475
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
-
1453
1476
.. method :: Path.absolute()
1454
1477
1455
1478
Make the path absolute, without normalization or resolving symlinks.
@@ -1489,25 +1512,6 @@ Other methods
1489
1512
The *strict * parameter was added (pre-3.6 behavior is strict).
1490
1513
1491
1514
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
-
1511
1515
1512
1516
Correspondence to tools in the :mod: `os ` module
1513
1517
-----------------------------------------------
0 commit comments