@@ -1343,6 +1343,92 @@ Reading directories
1343
1343
.. versionadded :: 3.12
1344
1344
1345
1345
1346
+ Creating files and directories
1347
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1348
+
1349
+ .. method :: Path.touch(mode=0o666, exist_ok=True)
1350
+
1351
+ Create a file at this given path. If *mode * is given, it is combined
1352
+ with the process's ``umask `` value to determine the file mode and access
1353
+ flags. If the file already exists, the function succeeds when *exist_ok *
1354
+ is true (and its modification time is updated to the current time),
1355
+ otherwise :exc: `FileExistsError ` is raised.
1356
+
1357
+ .. seealso ::
1358
+ The :meth: `~Path.open `, :meth: `~Path.write_text ` and
1359
+ :meth: `~Path.write_bytes ` methods are often used to create files.
1360
+
1361
+
1362
+ .. method :: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
1363
+
1364
+ Create a new directory at this given path. If *mode * is given, it is
1365
+ combined with the process's ``umask `` value to determine the file mode
1366
+ and access flags. If the path already exists, :exc: `FileExistsError `
1367
+ is raised.
1368
+
1369
+ If *parents * is true, any missing parents of this path are created
1370
+ as needed; they are created with the default permissions without taking
1371
+ *mode * into account (mimicking the POSIX ``mkdir -p `` command).
1372
+
1373
+ If *parents * is false (the default), a missing parent raises
1374
+ :exc: `FileNotFoundError `.
1375
+
1376
+ If *exist_ok * is false (the default), :exc: `FileExistsError ` is
1377
+ raised if the target directory already exists.
1378
+
1379
+ If *exist_ok * is true, :exc: `FileExistsError ` will not be raised unless the given
1380
+ path already exists in the file system and is not a directory (same
1381
+ behavior as the POSIX ``mkdir -p `` command).
1382
+
1383
+ .. versionchanged :: 3.5
1384
+ The *exist_ok * parameter was added.
1385
+
1386
+
1387
+ .. method :: Path.symlink_to(target, target_is_directory=False)
1388
+
1389
+ Make this path a symbolic link pointing to *target *.
1390
+
1391
+ On Windows, a symlink represents either a file or a directory, and does not
1392
+ morph to the target dynamically. If the target is present, the type of the
1393
+ symlink will be created to match. Otherwise, the symlink will be created
1394
+ as a directory if *target_is_directory * is true or a file symlink (the
1395
+ default) otherwise. On non-Windows platforms, *target_is_directory * is ignored.
1396
+
1397
+ ::
1398
+
1399
+ >>> p = Path('mylink')
1400
+ >>> p.symlink_to('setup.py')
1401
+ >>> p.resolve()
1402
+ PosixPath('/home/antoine/pathlib/setup.py')
1403
+ >>> p.stat().st_size
1404
+ 956
1405
+ >>> p.lstat().st_size
1406
+ 8
1407
+
1408
+ .. note ::
1409
+ The order of arguments (link, target) is the reverse
1410
+ of :func: `os.symlink `'s.
1411
+
1412
+ .. versionchanged :: 3.13
1413
+ Raises :exc: `UnsupportedOperation ` if :func: `os.symlink ` is not
1414
+ available. In previous versions, :exc: `NotImplementedError ` was raised.
1415
+
1416
+
1417
+ .. method :: Path.hardlink_to(target)
1418
+
1419
+ Make this path a hard link to the same file as *target *.
1420
+
1421
+ .. note ::
1422
+ The order of arguments (link, target) is the reverse
1423
+ of :func: `os.link `'s.
1424
+
1425
+ .. versionadded :: 3.10
1426
+
1427
+ .. versionchanged :: 3.13
1428
+ Raises :exc: `UnsupportedOperation ` if :func: `os.link ` is not
1429
+ available. In previous versions, :exc: `NotImplementedError ` was raised.
1430
+
1431
+
1346
1432
Other methods
1347
1433
^^^^^^^^^^^^^
1348
1434
@@ -1426,31 +1512,6 @@ Other methods
1426
1512
symbolic link's mode is changed rather than its target's.
1427
1513
1428
1514
1429
- .. method :: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
1430
-
1431
- Create a new directory at this given path. If *mode * is given, it is
1432
- combined with the process' ``umask `` value to determine the file mode
1433
- and access flags. If the path already exists, :exc: `FileExistsError `
1434
- is raised.
1435
-
1436
- If *parents * is true, any missing parents of this path are created
1437
- as needed; they are created with the default permissions without taking
1438
- *mode * into account (mimicking the POSIX ``mkdir -p `` command).
1439
-
1440
- If *parents * is false (the default), a missing parent raises
1441
- :exc: `FileNotFoundError `.
1442
-
1443
- If *exist_ok * is false (the default), :exc: `FileExistsError ` is
1444
- raised if the target directory already exists.
1445
-
1446
- If *exist_ok * is true, :exc: `FileExistsError ` will not be raised unless the given
1447
- path already exists in the file system and is not a directory (same
1448
- behavior as the POSIX ``mkdir -p `` command).
1449
-
1450
- .. versionchanged :: 3.5
1451
- The *exist_ok * parameter was added.
1452
-
1453
-
1454
1515
.. method :: Path.owner(*, follow_symlinks=True)
1455
1516
1456
1517
Return the name of the user owning the file. :exc: `KeyError ` is raised
@@ -1572,60 +1633,6 @@ Other methods
1572
1633
Remove this directory. The directory must be empty.
1573
1634
1574
1635
1575
- .. method :: Path.symlink_to(target, target_is_directory=False)
1576
-
1577
- Make this path a symbolic link pointing to *target *.
1578
-
1579
- On Windows, a symlink represents either a file or a directory, and does not
1580
- morph to the target dynamically. If the target is present, the type of the
1581
- symlink will be created to match. Otherwise, the symlink will be created
1582
- as a directory if *target_is_directory * is ``True `` or a file symlink (the
1583
- default) otherwise. On non-Windows platforms, *target_is_directory * is ignored.
1584
-
1585
- ::
1586
-
1587
- >>> p = Path('mylink')
1588
- >>> p.symlink_to('setup.py')
1589
- >>> p.resolve()
1590
- PosixPath('/home/antoine/pathlib/setup.py')
1591
- >>> p.stat().st_size
1592
- 956
1593
- >>> p.lstat().st_size
1594
- 8
1595
-
1596
- .. note ::
1597
- The order of arguments (link, target) is the reverse
1598
- of :func: `os.symlink `'s.
1599
-
1600
- .. versionchanged :: 3.13
1601
- Raises :exc: `UnsupportedOperation ` if :func: `os.symlink ` is not
1602
- available. In previous versions, :exc: `NotImplementedError ` was raised.
1603
-
1604
-
1605
- .. method :: Path.hardlink_to(target)
1606
-
1607
- Make this path a hard link to the same file as *target *.
1608
-
1609
- .. note ::
1610
- The order of arguments (link, target) is the reverse
1611
- of :func: `os.link `'s.
1612
-
1613
- .. versionadded :: 3.10
1614
-
1615
- .. versionchanged :: 3.13
1616
- Raises :exc: `UnsupportedOperation ` if :func: `os.link ` is not
1617
- available. In previous versions, :exc: `NotImplementedError ` was raised.
1618
-
1619
-
1620
- .. method :: Path.touch(mode=0o666, exist_ok=True)
1621
-
1622
- Create a file at this given path. If *mode * is given, it is combined
1623
- with the process' ``umask `` value to determine the file mode and access
1624
- flags. If the file already exists, the function succeeds if *exist_ok *
1625
- is true (and its modification time is updated to the current time),
1626
- otherwise :exc: `FileExistsError ` is raised.
1627
-
1628
-
1629
1636
.. method :: Path.unlink(missing_ok=False)
1630
1637
1631
1638
Remove this file or symbolic link. If the path points to a directory,
0 commit comments