@@ -1238,6 +1238,84 @@ Reading directories
1238
1238
.. versionadded :: 3.12
1239
1239
1240
1240
1241
+ Creating files and directories
1242
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1243
+
1244
+ .. method :: Path.touch(mode=0o666, exist_ok=True)
1245
+
1246
+ Create a file at this given path. If *mode * is given, it is combined
1247
+ with the process's ``umask `` value to determine the file mode and access
1248
+ flags. If the file already exists, the function succeeds when *exist_ok *
1249
+ is true (and its modification time is updated to the current time),
1250
+ otherwise :exc: `FileExistsError ` is raised.
1251
+
1252
+ .. seealso ::
1253
+ The :meth: `~Path.open `, :meth: `~Path.write_text ` and
1254
+ :meth: `~Path.write_bytes ` methods are often used to create files.
1255
+
1256
+
1257
+ .. method :: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
1258
+
1259
+ Create a new directory at this given path. If *mode * is given, it is
1260
+ combined with the process's ``umask `` value to determine the file mode
1261
+ and access flags. If the path already exists, :exc: `FileExistsError `
1262
+ is raised.
1263
+
1264
+ If *parents * is true, any missing parents of this path are created
1265
+ as needed; they are created with the default permissions without taking
1266
+ *mode * into account (mimicking the POSIX ``mkdir -p `` command).
1267
+
1268
+ If *parents * is false (the default), a missing parent raises
1269
+ :exc: `FileNotFoundError `.
1270
+
1271
+ If *exist_ok * is false (the default), :exc: `FileExistsError ` is
1272
+ raised if the target directory already exists.
1273
+
1274
+ If *exist_ok * is true, :exc: `FileExistsError ` will not be raised unless the given
1275
+ path already exists in the file system and is not a directory (same
1276
+ behavior as the POSIX ``mkdir -p `` command).
1277
+
1278
+ .. versionchanged :: 3.5
1279
+ The *exist_ok * parameter was added.
1280
+
1281
+
1282
+ .. method :: Path.symlink_to(target, target_is_directory=False)
1283
+
1284
+ Make this path a symbolic link pointing to *target *.
1285
+
1286
+ On Windows, a symlink represents either a file or a directory, and does not
1287
+ morph to the target dynamically. If the target is present, the type of the
1288
+ symlink will be created to match. Otherwise, the symlink will be created
1289
+ as a directory if *target_is_directory * is true or a file symlink (the
1290
+ default) otherwise. On non-Windows platforms, *target_is_directory * is ignored.
1291
+
1292
+ ::
1293
+
1294
+ >>> p = Path('mylink')
1295
+ >>> p.symlink_to('setup.py')
1296
+ >>> p.resolve()
1297
+ PosixPath('/home/antoine/pathlib/setup.py')
1298
+ >>> p.stat().st_size
1299
+ 956
1300
+ >>> p.lstat().st_size
1301
+ 8
1302
+
1303
+ .. note ::
1304
+ The order of arguments (link, target) is the reverse
1305
+ of :func: `os.symlink `'s.
1306
+
1307
+
1308
+ .. method :: Path.hardlink_to(target)
1309
+
1310
+ Make this path a hard link to the same file as *target *.
1311
+
1312
+ .. note ::
1313
+ The order of arguments (link, target) is the reverse
1314
+ of :func: `os.link `'s.
1315
+
1316
+ .. versionadded :: 3.10
1317
+
1318
+
1241
1319
Other methods
1242
1320
^^^^^^^^^^^^^
1243
1321
@@ -1312,31 +1390,6 @@ Other methods
1312
1390
symbolic link's mode is changed rather than its target's.
1313
1391
1314
1392
1315
- .. method :: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
1316
-
1317
- Create a new directory at this given path. If *mode * is given, it is
1318
- combined with the process' ``umask `` value to determine the file mode
1319
- and access flags. If the path already exists, :exc: `FileExistsError `
1320
- is raised.
1321
-
1322
- If *parents * is true, any missing parents of this path are created
1323
- as needed; they are created with the default permissions without taking
1324
- *mode * into account (mimicking the POSIX ``mkdir -p `` command).
1325
-
1326
- If *parents * is false (the default), a missing parent raises
1327
- :exc: `FileNotFoundError `.
1328
-
1329
- If *exist_ok * is false (the default), :exc: `FileExistsError ` is
1330
- raised if the target directory already exists.
1331
-
1332
- If *exist_ok * is true, :exc: `FileExistsError ` will not be raised unless the given
1333
- path already exists in the file system and is not a directory (same
1334
- behavior as the POSIX ``mkdir -p `` command).
1335
-
1336
- .. versionchanged :: 3.5
1337
- The *exist_ok * parameter was added.
1338
-
1339
-
1340
1393
.. method :: Path.owner()
1341
1394
1342
1395
Return the name of the user owning the file. :exc: `KeyError ` is raised
@@ -1441,51 +1494,6 @@ Other methods
1441
1494
Remove this directory. The directory must be empty.
1442
1495
1443
1496
1444
- .. method :: Path.symlink_to(target, target_is_directory=False)
1445
-
1446
- Make this path a symbolic link pointing to *target *.
1447
-
1448
- On Windows, a symlink represents either a file or a directory, and does not
1449
- morph to the target dynamically. If the target is present, the type of the
1450
- symlink will be created to match. Otherwise, the symlink will be created
1451
- as a directory if *target_is_directory * is ``True `` or a file symlink (the
1452
- default) otherwise. On non-Windows platforms, *target_is_directory * is ignored.
1453
-
1454
- ::
1455
-
1456
- >>> p = Path('mylink')
1457
- >>> p.symlink_to('setup.py')
1458
- >>> p.resolve()
1459
- PosixPath('/home/antoine/pathlib/setup.py')
1460
- >>> p.stat().st_size
1461
- 956
1462
- >>> p.lstat().st_size
1463
- 8
1464
-
1465
- .. note ::
1466
- The order of arguments (link, target) is the reverse
1467
- of :func: `os.symlink `'s.
1468
-
1469
- .. method :: Path.hardlink_to(target)
1470
-
1471
- Make this path a hard link to the same file as *target *.
1472
-
1473
- .. note ::
1474
- The order of arguments (link, target) is the reverse
1475
- of :func: `os.link `'s.
1476
-
1477
- .. versionadded :: 3.10
1478
-
1479
-
1480
- .. method :: Path.touch(mode=0o666, exist_ok=True)
1481
-
1482
- Create a file at this given path. If *mode * is given, it is combined
1483
- with the process' ``umask `` value to determine the file mode and access
1484
- flags. If the file already exists, the function succeeds if *exist_ok *
1485
- is true (and its modification time is updated to the current time),
1486
- otherwise :exc: `FileExistsError ` is raised.
1487
-
1488
-
1489
1497
.. method :: Path.unlink(missing_ok=False)
1490
1498
1491
1499
Remove this file or symbolic link. If the path points to a directory,
0 commit comments