Skip to content

Commit e0f4dd8

Browse files
miss-islingtonbarneygalehugovk
authored
[3.13] GH-119054: Add "Creating files and directories" section to pathlib docs. (GH-120186) (#120462)
GH-119054: Add "Creating files and directories" section to pathlib docs. (GH-120186) 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) Co-authored-by: Barney Gale <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 5ce72e2 commit e0f4dd8

File tree

1 file changed

+86
-79
lines changed

1 file changed

+86
-79
lines changed

Doc/library/pathlib.rst

+86-79
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,92 @@ Reading directories
13251325
.. versionadded:: 3.12
13261326

13271327

1328+
Creating files and directories
1329+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1330+
1331+
.. method:: Path.touch(mode=0o666, exist_ok=True)
1332+
1333+
Create a file at this given path. If *mode* is given, it is combined
1334+
with the process's ``umask`` value to determine the file mode and access
1335+
flags. If the file already exists, the function succeeds when *exist_ok*
1336+
is true (and its modification time is updated to the current time),
1337+
otherwise :exc:`FileExistsError` is raised.
1338+
1339+
.. seealso::
1340+
The :meth:`~Path.open`, :meth:`~Path.write_text` and
1341+
:meth:`~Path.write_bytes` methods are often used to create files.
1342+
1343+
1344+
.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
1345+
1346+
Create a new directory at this given path. If *mode* is given, it is
1347+
combined with the process's ``umask`` value to determine the file mode
1348+
and access flags. If the path already exists, :exc:`FileExistsError`
1349+
is raised.
1350+
1351+
If *parents* is true, any missing parents of this path are created
1352+
as needed; they are created with the default permissions without taking
1353+
*mode* into account (mimicking the POSIX ``mkdir -p`` command).
1354+
1355+
If *parents* is false (the default), a missing parent raises
1356+
:exc:`FileNotFoundError`.
1357+
1358+
If *exist_ok* is false (the default), :exc:`FileExistsError` is
1359+
raised if the target directory already exists.
1360+
1361+
If *exist_ok* is true, :exc:`FileExistsError` will not be raised unless the given
1362+
path already exists in the file system and is not a directory (same
1363+
behavior as the POSIX ``mkdir -p`` command).
1364+
1365+
.. versionchanged:: 3.5
1366+
The *exist_ok* parameter was added.
1367+
1368+
1369+
.. method:: Path.symlink_to(target, target_is_directory=False)
1370+
1371+
Make this path a symbolic link pointing to *target*.
1372+
1373+
On Windows, a symlink represents either a file or a directory, and does not
1374+
morph to the target dynamically. If the target is present, the type of the
1375+
symlink will be created to match. Otherwise, the symlink will be created
1376+
as a directory if *target_is_directory* is true or a file symlink (the
1377+
default) otherwise. On non-Windows platforms, *target_is_directory* is ignored.
1378+
1379+
::
1380+
1381+
>>> p = Path('mylink')
1382+
>>> p.symlink_to('setup.py')
1383+
>>> p.resolve()
1384+
PosixPath('/home/antoine/pathlib/setup.py')
1385+
>>> p.stat().st_size
1386+
956
1387+
>>> p.lstat().st_size
1388+
8
1389+
1390+
.. note::
1391+
The order of arguments (link, target) is the reverse
1392+
of :func:`os.symlink`'s.
1393+
1394+
.. versionchanged:: 3.13
1395+
Raises :exc:`UnsupportedOperation` if :func:`os.symlink` is not
1396+
available. In previous versions, :exc:`NotImplementedError` was raised.
1397+
1398+
1399+
.. method:: Path.hardlink_to(target)
1400+
1401+
Make this path a hard link to the same file as *target*.
1402+
1403+
.. note::
1404+
The order of arguments (link, target) is the reverse
1405+
of :func:`os.link`'s.
1406+
1407+
.. versionadded:: 3.10
1408+
1409+
.. versionchanged:: 3.13
1410+
Raises :exc:`UnsupportedOperation` if :func:`os.link` is not
1411+
available. In previous versions, :exc:`NotImplementedError` was raised.
1412+
1413+
13281414
Other methods
13291415
^^^^^^^^^^^^^
13301416

@@ -1409,31 +1495,6 @@ Other methods
14091495
symbolic link's mode is changed rather than its target's.
14101496

14111497

1412-
.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
1413-
1414-
Create a new directory at this given path. If *mode* is given, it is
1415-
combined with the process' ``umask`` value to determine the file mode
1416-
and access flags. If the path already exists, :exc:`FileExistsError`
1417-
is raised.
1418-
1419-
If *parents* is true, any missing parents of this path are created
1420-
as needed; they are created with the default permissions without taking
1421-
*mode* into account (mimicking the POSIX ``mkdir -p`` command).
1422-
1423-
If *parents* is false (the default), a missing parent raises
1424-
:exc:`FileNotFoundError`.
1425-
1426-
If *exist_ok* is false (the default), :exc:`FileExistsError` is
1427-
raised if the target directory already exists.
1428-
1429-
If *exist_ok* is true, :exc:`FileExistsError` will not be raised unless the given
1430-
path already exists in the file system and is not a directory (same
1431-
behavior as the POSIX ``mkdir -p`` command).
1432-
1433-
.. versionchanged:: 3.5
1434-
The *exist_ok* parameter was added.
1435-
1436-
14371498
.. method:: Path.owner(*, follow_symlinks=True)
14381499

14391500
Return the name of the user owning the file. :exc:`KeyError` is raised
@@ -1555,60 +1616,6 @@ Other methods
15551616
Remove this directory. The directory must be empty.
15561617

15571618

1558-
.. method:: Path.symlink_to(target, target_is_directory=False)
1559-
1560-
Make this path a symbolic link pointing to *target*.
1561-
1562-
On Windows, a symlink represents either a file or a directory, and does not
1563-
morph to the target dynamically. If the target is present, the type of the
1564-
symlink will be created to match. Otherwise, the symlink will be created
1565-
as a directory if *target_is_directory* is ``True`` or a file symlink (the
1566-
default) otherwise. On non-Windows platforms, *target_is_directory* is ignored.
1567-
1568-
::
1569-
1570-
>>> p = Path('mylink')
1571-
>>> p.symlink_to('setup.py')
1572-
>>> p.resolve()
1573-
PosixPath('/home/antoine/pathlib/setup.py')
1574-
>>> p.stat().st_size
1575-
956
1576-
>>> p.lstat().st_size
1577-
8
1578-
1579-
.. note::
1580-
The order of arguments (link, target) is the reverse
1581-
of :func:`os.symlink`'s.
1582-
1583-
.. versionchanged:: 3.13
1584-
Raises :exc:`UnsupportedOperation` if :func:`os.symlink` is not
1585-
available. In previous versions, :exc:`NotImplementedError` was raised.
1586-
1587-
1588-
.. method:: Path.hardlink_to(target)
1589-
1590-
Make this path a hard link to the same file as *target*.
1591-
1592-
.. note::
1593-
The order of arguments (link, target) is the reverse
1594-
of :func:`os.link`'s.
1595-
1596-
.. versionadded:: 3.10
1597-
1598-
.. versionchanged:: 3.13
1599-
Raises :exc:`UnsupportedOperation` if :func:`os.link` is not
1600-
available. In previous versions, :exc:`NotImplementedError` was raised.
1601-
1602-
1603-
.. method:: Path.touch(mode=0o666, exist_ok=True)
1604-
1605-
Create a file at this given path. If *mode* is given, it is combined
1606-
with the process' ``umask`` value to determine the file mode and access
1607-
flags. If the file already exists, the function succeeds if *exist_ok*
1608-
is true (and its modification time is updated to the current time),
1609-
otherwise :exc:`FileExistsError` is raised.
1610-
1611-
16121619
.. method:: Path.unlink(missing_ok=False)
16131620

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

0 commit comments

Comments
 (0)