Skip to content

Commit eff0553

Browse files
authored
[3.12] GH-119054: Add "Creating files and directories" section to pathlib docs. (GH-120186) (#120464)
Add dedicated subsection for `pathlib.Path.touch()`, `mkdir()`, `symlink_to()` and `hardlink_to()`. Also note that `open()`, `write_text()` and `write_bytes()` are often used to create files. (cherry picked from commit c2d810b)
1 parent f8dd4c3 commit eff0553

File tree

1 file changed

+78
-70
lines changed

1 file changed

+78
-70
lines changed

Doc/library/pathlib.rst

+78-70
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,84 @@ Reading directories
12381238
.. versionadded:: 3.12
12391239

12401240

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+
12411319
Other methods
12421320
^^^^^^^^^^^^^
12431321

@@ -1312,31 +1390,6 @@ Other methods
13121390
symbolic link's mode is changed rather than its target's.
13131391

13141392

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-
13401393
.. method:: Path.owner()
13411394

13421395
Return the name of the user owning the file. :exc:`KeyError` is raised
@@ -1441,51 +1494,6 @@ Other methods
14411494
Remove this directory. The directory must be empty.
14421495

14431496

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-
14891497
.. method:: Path.unlink(missing_ok=False)
14901498

14911499
Remove this file or symbolic link. If the path points to a directory,

0 commit comments

Comments
 (0)