Skip to content

Commit c2d810b

Browse files
barneygalehugovk
andauthored
GH-119054: Add "Creating files and directories" section to pathlib docs. (#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. Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 6af190f commit c2d810b

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
@@ -1343,6 +1343,92 @@ Reading directories
13431343
.. versionadded:: 3.12
13441344

13451345

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+
13461432
Other methods
13471433
^^^^^^^^^^^^^
13481434

@@ -1426,31 +1512,6 @@ Other methods
14261512
symbolic link's mode is changed rather than its target's.
14271513

14281514

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-
14541515
.. method:: Path.owner(*, follow_symlinks=True)
14551516

14561517
Return the name of the user owning the file. :exc:`KeyError` is raised
@@ -1572,60 +1633,6 @@ Other methods
15721633
Remove this directory. The directory must be empty.
15731634

15741635

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

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

0 commit comments

Comments
 (0)