Skip to content

Commit 81d6336

Browse files
authored
GH-119054: Add "Querying file type and status" section to pathlib docs (#119055)
Add a dedicated subsection for `Path.stat()`-related methods, specifically `stat()`, `lstat()`, `exists()`, `is_*()`, and `samefile()`.
1 parent 045e195 commit 81d6336

File tree

1 file changed

+171
-167
lines changed

1 file changed

+171
-167
lines changed

Doc/library/pathlib.rst

+171-167
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,8 @@ bugs or failures in your application)::
808808
UnsupportedOperation: cannot instantiate 'WindowsPath' on your system
809809

810810

811-
File URIs
812-
^^^^^^^^^
811+
Parsing and generating URIs
812+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
813813

814814
Concrete path objects can be created from, and represented as, 'file' URIs
815815
conforming to :rfc:`8089`.
@@ -869,12 +869,8 @@ conforming to :rfc:`8089`.
869869
it strictly impure.
870870

871871

872-
Methods
873-
^^^^^^^
874-
875-
Concrete paths provide the following methods in addition to pure paths
876-
methods. Some of these methods can raise an :exc:`OSError` if a system
877-
call fails (for example because the path doesn't exist).
872+
Querying file type and status
873+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
878874

879875
.. versionchanged:: 3.8
880876

@@ -895,29 +891,6 @@ call fails (for example because the path doesn't exist).
895891
status without suppressing exceptions.
896892

897893

898-
.. classmethod:: Path.cwd()
899-
900-
Return a new path object representing the current directory (as returned
901-
by :func:`os.getcwd`)::
902-
903-
>>> Path.cwd()
904-
PosixPath('/home/antoine/pathlib')
905-
906-
907-
.. classmethod:: Path.home()
908-
909-
Return a new path object representing the user's home directory (as
910-
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
911-
directory can't be resolved, :exc:`RuntimeError` is raised.
912-
913-
::
914-
915-
>>> Path.home()
916-
PosixPath('/home/antoine')
917-
918-
.. versionadded:: 3.5
919-
920-
921894
.. method:: Path.stat(*, follow_symlinks=True)
922895

923896
Return a :class:`os.stat_result` object containing information about this path, like :func:`os.stat`.
@@ -937,25 +910,12 @@ call fails (for example because the path doesn't exist).
937910
.. versionchanged:: 3.10
938911
The *follow_symlinks* parameter was added.
939912

940-
.. method:: Path.chmod(mode, *, follow_symlinks=True)
941-
942-
Change the file mode and permissions, like :func:`os.chmod`.
943-
944-
This method normally follows symlinks. Some Unix flavours support changing
945-
permissions on the symlink itself; on these platforms you may add the
946-
argument ``follow_symlinks=False``, or use :meth:`~Path.lchmod`.
947913

948-
::
914+
.. method:: Path.lstat()
949915

950-
>>> p = Path('setup.py')
951-
>>> p.stat().st_mode
952-
33277
953-
>>> p.chmod(0o444)
954-
>>> p.stat().st_mode
955-
33060
916+
Like :meth:`Path.stat` but, if the path points to a symbolic link, return
917+
the symbolic link's information rather than its target's.
956918

957-
.. versionchanged:: 3.10
958-
The *follow_symlinks* parameter was added.
959919

960920
.. method:: Path.exists(*, follow_symlinks=True)
961921

@@ -980,6 +940,170 @@ call fails (for example because the path doesn't exist).
980940
.. versionchanged:: 3.12
981941
The *follow_symlinks* parameter was added.
982942

943+
944+
.. method:: Path.is_file(*, follow_symlinks=True)
945+
946+
Return ``True`` if the path points to a regular file. ``False`` will be
947+
returned if the path is invalid, inaccessible or missing, or if it points
948+
to something other than a regular file. Use :meth:`Path.stat` to
949+
distinguish between these cases.
950+
951+
This method normally follows symlinks; to exclude symlinks, add the
952+
argument ``follow_symlinks=False``.
953+
954+
.. versionchanged:: 3.13
955+
The *follow_symlinks* parameter was added.
956+
957+
958+
.. method:: Path.is_dir(*, follow_symlinks=True)
959+
960+
Return ``True`` if the path points to a directory. ``False`` will be
961+
returned if the path is invalid, inaccessible or missing, or if it points
962+
to something other than a directory. Use :meth:`Path.stat` to distinguish
963+
between these cases.
964+
965+
This method normally follows symlinks; to exclude symlinks to directories,
966+
add the argument ``follow_symlinks=False``.
967+
968+
.. versionchanged:: 3.13
969+
The *follow_symlinks* parameter was added.
970+
971+
972+
.. method:: Path.is_symlink()
973+
974+
Return ``True`` if the path points to a symbolic link, even if that symlink
975+
is broken. ``False`` will be returned if the path is invalid, inaccessible
976+
or missing, or if it points to something other than a symbolic link. Use
977+
:meth:`Path.stat` to distinguish between these cases.
978+
979+
980+
.. method:: Path.is_junction()
981+
982+
Return ``True`` if the path points to a junction, and ``False`` for any other
983+
type of file. Currently only Windows supports junctions.
984+
985+
.. versionadded:: 3.12
986+
987+
988+
.. method:: Path.is_mount()
989+
990+
Return ``True`` if the path is a :dfn:`mount point`: a point in a
991+
file system where a different file system has been mounted. On POSIX, the
992+
function checks whether *path*'s parent, :file:`path/..`, is on a different
993+
device than *path*, or whether :file:`path/..` and *path* point to the same
994+
i-node on the same device --- this should detect mount points for all Unix
995+
and POSIX variants. On Windows, a mount point is considered to be a drive
996+
letter root (e.g. ``c:\``), a UNC share (e.g. ``\\server\share``), or a
997+
mounted filesystem directory.
998+
999+
.. versionadded:: 3.7
1000+
1001+
.. versionchanged:: 3.12
1002+
Windows support was added.
1003+
1004+
.. method:: Path.is_socket()
1005+
1006+
Return ``True`` if the path points to a Unix socket. ``False`` will be
1007+
returned if the path is invalid, inaccessible or missing, or if it points
1008+
to something other than a Unix socket. Use :meth:`Path.stat` to
1009+
distinguish between these cases.
1010+
1011+
1012+
.. method:: Path.is_fifo()
1013+
1014+
Return ``True`` if the path points to a FIFO. ``False`` will be returned if
1015+
the path is invalid, inaccessible or missing, or if it points to something
1016+
other than a FIFO. Use :meth:`Path.stat` to distinguish between these
1017+
cases.
1018+
1019+
1020+
.. method:: Path.is_block_device()
1021+
1022+
Return ``True`` if the path points to a block device. ``False`` will be
1023+
returned if the path is invalid, inaccessible or missing, or if it points
1024+
to something other than a block device. Use :meth:`Path.stat` to
1025+
distinguish between these cases.
1026+
1027+
1028+
.. method:: Path.is_char_device()
1029+
1030+
Return ``True`` if the path points to a character device. ``False`` will be
1031+
returned if the path is invalid, inaccessible or missing, or if it points
1032+
to something other than a character device. Use :meth:`Path.stat` to
1033+
distinguish between these cases.
1034+
1035+
1036+
.. method:: Path.samefile(other_path)
1037+
1038+
Return whether this path points to the same file as *other_path*, which
1039+
can be either a Path object, or a string. The semantics are similar
1040+
to :func:`os.path.samefile` and :func:`os.path.samestat`.
1041+
1042+
An :exc:`OSError` can be raised if either file cannot be accessed for some
1043+
reason.
1044+
1045+
::
1046+
1047+
>>> p = Path('spam')
1048+
>>> q = Path('eggs')
1049+
>>> p.samefile(q)
1050+
False
1051+
>>> p.samefile('spam')
1052+
True
1053+
1054+
.. versionadded:: 3.5
1055+
1056+
1057+
Other methods
1058+
^^^^^^^^^^^^^
1059+
1060+
Some of these methods can raise an :exc:`OSError` if a system call fails (for
1061+
example because the path doesn't exist).
1062+
1063+
1064+
.. classmethod:: Path.cwd()
1065+
1066+
Return a new path object representing the current directory (as returned
1067+
by :func:`os.getcwd`)::
1068+
1069+
>>> Path.cwd()
1070+
PosixPath('/home/antoine/pathlib')
1071+
1072+
1073+
.. classmethod:: Path.home()
1074+
1075+
Return a new path object representing the user's home directory (as
1076+
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1077+
directory can't be resolved, :exc:`RuntimeError` is raised.
1078+
1079+
::
1080+
1081+
>>> Path.home()
1082+
PosixPath('/home/antoine')
1083+
1084+
.. versionadded:: 3.5
1085+
1086+
1087+
.. method:: Path.chmod(mode, *, follow_symlinks=True)
1088+
1089+
Change the file mode and permissions, like :func:`os.chmod`.
1090+
1091+
This method normally follows symlinks. Some Unix flavours support changing
1092+
permissions on the symlink itself; on these platforms you may add the
1093+
argument ``follow_symlinks=False``, or use :meth:`~Path.lchmod`.
1094+
1095+
::
1096+
1097+
>>> p = Path('setup.py')
1098+
>>> p.stat().st_mode
1099+
33277
1100+
>>> p.chmod(0o444)
1101+
>>> p.stat().st_mode
1102+
33060
1103+
1104+
.. versionchanged:: 3.10
1105+
The *follow_symlinks* parameter was added.
1106+
9831107
.. method:: Path.expanduser()
9841108

9851109
Return a new path with expanded ``~`` and ``~user`` constructs,
@@ -1076,99 +1200,6 @@ call fails (for example because the path doesn't exist).
10761200
The *follow_symlinks* parameter was added.
10771201

10781202

1079-
.. method:: Path.is_dir(*, follow_symlinks=True)
1080-
1081-
Return ``True`` if the path points to a directory. ``False`` will be
1082-
returned if the path is invalid, inaccessible or missing, or if it points
1083-
to something other than a directory. Use :meth:`Path.stat` to distinguish
1084-
between these cases.
1085-
1086-
This method normally follows symlinks; to exclude symlinks to directories,
1087-
add the argument ``follow_symlinks=False``.
1088-
1089-
.. versionchanged:: 3.13
1090-
The *follow_symlinks* parameter was added.
1091-
1092-
1093-
.. method:: Path.is_file(*, follow_symlinks=True)
1094-
1095-
Return ``True`` if the path points to a regular file. ``False`` will be
1096-
returned if the path is invalid, inaccessible or missing, or if it points
1097-
to something other than a regular file. Use :meth:`Path.stat` to
1098-
distinguish between these cases.
1099-
1100-
This method normally follows symlinks; to exclude symlinks, add the
1101-
argument ``follow_symlinks=False``.
1102-
1103-
.. versionchanged:: 3.13
1104-
The *follow_symlinks* parameter was added.
1105-
1106-
1107-
.. method:: Path.is_junction()
1108-
1109-
Return ``True`` if the path points to a junction, and ``False`` for any other
1110-
type of file. Currently only Windows supports junctions.
1111-
1112-
.. versionadded:: 3.12
1113-
1114-
1115-
.. method:: Path.is_mount()
1116-
1117-
Return ``True`` if the path is a :dfn:`mount point`: a point in a
1118-
file system where a different file system has been mounted. On POSIX, the
1119-
function checks whether *path*'s parent, :file:`path/..`, is on a different
1120-
device than *path*, or whether :file:`path/..` and *path* point to the same
1121-
i-node on the same device --- this should detect mount points for all Unix
1122-
and POSIX variants. On Windows, a mount point is considered to be a drive
1123-
letter root (e.g. ``c:\``), a UNC share (e.g. ``\\server\share``), or a
1124-
mounted filesystem directory.
1125-
1126-
.. versionadded:: 3.7
1127-
1128-
.. versionchanged:: 3.12
1129-
Windows support was added.
1130-
1131-
1132-
.. method:: Path.is_symlink()
1133-
1134-
Return ``True`` if the path points to a symbolic link, even if that symlink
1135-
is broken. ``False`` will be returned if the path is invalid, inaccessible
1136-
or missing, or if it points to something other than a symbolic link. Use
1137-
:meth:`Path.stat` to distinguish between these cases.
1138-
1139-
1140-
.. method:: Path.is_socket()
1141-
1142-
Return ``True`` if the path points to a Unix socket. ``False`` will be
1143-
returned if the path is invalid, inaccessible or missing, or if it points
1144-
to something other than a Unix socket. Use :meth:`Path.stat` to
1145-
distinguish between these cases.
1146-
1147-
1148-
.. method:: Path.is_fifo()
1149-
1150-
Return ``True`` if the path points to a FIFO. ``False`` will be returned if
1151-
the path is invalid, inaccessible or missing, or if it points to something
1152-
other than a FIFO. Use :meth:`Path.stat` to distinguish between these
1153-
cases.
1154-
1155-
1156-
.. method:: Path.is_block_device()
1157-
1158-
Return ``True`` if the path points to a block device. ``False`` will be
1159-
returned if the path is invalid, inaccessible or missing, or if it points
1160-
to something other than a block device. Use :meth:`Path.stat` to
1161-
distinguish between these cases.
1162-
1163-
1164-
.. method:: Path.is_char_device()
1165-
1166-
Return ``True`` if the path points to a character device. ``False`` will be
1167-
returned if the path is invalid, inaccessible or missing, or if it points
1168-
to something other than a character device. Use :meth:`Path.stat` to
1169-
distinguish between these cases.
1170-
1171-
11721203
.. method:: Path.iterdir()
11731204

11741205
When the path points to a directory, yield path objects of the directory
@@ -1291,12 +1322,6 @@ call fails (for example because the path doesn't exist).
12911322
symbolic link's mode is changed rather than its target's.
12921323

12931324

1294-
.. method:: Path.lstat()
1295-
1296-
Like :meth:`Path.stat` but, if the path points to a symbolic link, return
1297-
the symbolic link's information rather than its target's.
1298-
1299-
13001325
.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
13011326

13021327
Create a new directory at this given path. If *mode* is given, it is
@@ -1486,27 +1511,6 @@ call fails (for example because the path doesn't exist).
14861511
Remove this directory. The directory must be empty.
14871512

14881513

1489-
.. method:: Path.samefile(other_path)
1490-
1491-
Return whether this path points to the same file as *other_path*, which
1492-
can be either a Path object, or a string. The semantics are similar
1493-
to :func:`os.path.samefile` and :func:`os.path.samestat`.
1494-
1495-
An :exc:`OSError` can be raised if either file cannot be accessed for some
1496-
reason.
1497-
1498-
::
1499-
1500-
>>> p = Path('spam')
1501-
>>> q = Path('eggs')
1502-
>>> p.samefile(q)
1503-
False
1504-
>>> p.samefile('spam')
1505-
True
1506-
1507-
.. versionadded:: 3.5
1508-
1509-
15101514
.. method:: Path.symlink_to(target, target_is_directory=False)
15111515

15121516
Make this path a symbolic link pointing to *target*.

0 commit comments

Comments
 (0)