@@ -789,6 +789,9 @@ bugs or failures in your application)::
789
789
% (cls.__name__,))
790
790
NotImplementedError: cannot instantiate 'WindowsPath' on your system
791
791
792
+ Some concrete path methods can raise an :exc: `OSError ` if a system call fails
793
+ (for example because the path doesn't exist).
794
+
792
795
793
796
Querying file type and status
794
797
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1040,71 +1043,32 @@ Reading and writing files
1040
1043
.. versionadded :: 3.5
1041
1044
1042
1045
1043
- Other methods
1044
- ^^^^^^^^^^^^^
1045
-
1046
- Many of these methods can raise an :exc: `OSError ` if a system call fails (for
1047
- example because the path doesn't exist).
1048
-
1049
-
1050
- .. classmethod :: Path.cwd()
1051
-
1052
- Return a new path object representing the current directory (as returned
1053
- by :func: `os.getcwd `)::
1054
-
1055
- >>> Path.cwd()
1056
- PosixPath('/home/antoine/pathlib')
1057
-
1058
-
1059
- .. classmethod :: Path.home()
1060
-
1061
- Return a new path object representing the user's home directory (as
1062
- returned by :func: `os.path.expanduser ` with ``~ `` construct). If the home
1063
- directory can't be resolved, :exc: `RuntimeError ` is raised.
1064
-
1065
- ::
1066
-
1067
- >>> Path.home()
1068
- PosixPath('/home/antoine')
1069
-
1070
- .. versionadded :: 3.5
1046
+ Reading directories
1047
+ ^^^^^^^^^^^^^^^^^^^
1071
1048
1049
+ .. method :: Path.iterdir()
1072
1050
1073
- .. method :: Path.chmod(mode, *, follow_symlinks=True)
1074
-
1075
- Change the file mode and permissions, like :func: `os.chmod `.
1076
-
1077
- This method normally follows symlinks. Some Unix flavours support changing
1078
- permissions on the symlink itself; on these platforms you may add the
1079
- argument ``follow_symlinks=False ``, or use :meth: `~Path.lchmod `.
1080
-
1081
- ::
1082
-
1083
- >>> p = Path('setup.py')
1084
- >>> p.stat().st_mode
1085
- 33277
1086
- >>> p.chmod(0o444)
1087
- >>> p.stat().st_mode
1088
- 33060
1089
-
1090
- .. versionchanged :: 3.10
1091
- The *follow_symlinks * parameter was added.
1092
-
1093
-
1094
- .. method :: Path.expanduser()
1095
-
1096
- Return a new path with expanded ``~ `` and ``~user `` constructs,
1097
- as returned by :meth: `os.path.expanduser `. If a home directory can't be
1098
- resolved, :exc: `RuntimeError ` is raised.
1099
-
1100
- ::
1051
+ When the path points to a directory, yield path objects of the directory
1052
+ contents::
1101
1053
1102
- >>> p = PosixPath('~/films/Monty Python')
1103
- >>> p.expanduser()
1104
- PosixPath('/home/eric/films/Monty Python')
1054
+ >>> p = Path('docs')
1055
+ >>> for child in p.iterdir(): child
1056
+ ...
1057
+ PosixPath('docs/conf.py')
1058
+ PosixPath('docs/_templates')
1059
+ PosixPath('docs/make.bat')
1060
+ PosixPath('docs/index.rst')
1061
+ PosixPath('docs/_build')
1062
+ PosixPath('docs/_static')
1063
+ PosixPath('docs/Makefile')
1105
1064
1106
- .. versionadded :: 3.5
1065
+ The children are yielded in arbitrary order, and the special entries
1066
+ ``'.' `` and ``'..' `` are not included. If a file is removed from or added
1067
+ to the directory after creating the iterator, it is unspecified whether
1068
+ a path object for that file is included.
1107
1069
1070
+ If the path is not a directory or otherwise inaccessible, :exc: `OSError ` is
1071
+ raised.
1108
1072
1109
1073
.. method :: Path.glob(pattern, *, case_sensitive=None)
1110
1074
@@ -1150,32 +1114,33 @@ example because the path doesn't exist).
1150
1114
The *case_sensitive * parameter was added.
1151
1115
1152
1116
1153
- .. method :: Path.group( )
1117
+ .. method :: Path.rglob(pattern, *, case_sensitive=None )
1154
1118
1155
- Return the name of the group owning the file. :exc: `KeyError ` is raised
1156
- if the file's gid isn't found in the system database.
1119
+ Glob the given relative *pattern * recursively. This is like calling
1120
+ :func: `Path.glob ` with "``**/ ``" added in front of the *pattern *, where
1121
+ *patterns * are the same as for :mod: `fnmatch `::
1157
1122
1123
+ >>> sorted(Path().rglob("*.py"))
1124
+ [PosixPath('build/lib/pathlib.py'),
1125
+ PosixPath('docs/conf.py'),
1126
+ PosixPath('pathlib.py'),
1127
+ PosixPath('setup.py'),
1128
+ PosixPath('test_pathlib.py')]
1158
1129
1159
- .. method :: Path.iterdir()
1130
+ By default, or when the *case_sensitive * keyword-only argument is set to
1131
+ ``None ``, this method matches paths using platform-specific casing rules:
1132
+ typically, case-sensitive on POSIX, and case-insensitive on Windows.
1133
+ Set *case_sensitive * to ``True `` or ``False `` to override this behaviour.
1160
1134
1161
- When the path points to a directory, yield path objects of the directory
1162
- contents::
1135
+ .. audit-event :: pathlib.Path.rglob self,pattern pathlib.Path.rglob
1163
1136
1164
- >>> p = Path('docs')
1165
- >>> for child in p.iterdir(): child
1166
- ...
1167
- PosixPath('docs/conf.py')
1168
- PosixPath('docs/_templates')
1169
- PosixPath('docs/make.bat')
1170
- PosixPath('docs/index.rst')
1171
- PosixPath('docs/_build')
1172
- PosixPath('docs/_static')
1173
- PosixPath('docs/Makefile')
1137
+ .. versionchanged :: 3.11
1138
+ Return only directories if *pattern * ends with a pathname components
1139
+ separator (:data: `~os.sep ` or :data: `~os.altsep `).
1140
+
1141
+ .. versionchanged :: 3.12
1142
+ The *case_sensitive * parameter was added.
1174
1143
1175
- The children are yielded in arbitrary order, and the special entries
1176
- ``'.' `` and ``'..' `` are not included. If a file is removed from or added
1177
- to the directory after creating the iterator, whether a path object for
1178
- that file be included is unspecified.
1179
1144
1180
1145
.. method :: Path.walk(top_down=True, on_error=None, follow_symlinks=False)
1181
1146
@@ -1272,6 +1237,75 @@ example because the path doesn't exist).
1272
1237
1273
1238
.. versionadded :: 3.12
1274
1239
1240
+
1241
+ Other methods
1242
+ ^^^^^^^^^^^^^
1243
+
1244
+ .. classmethod :: Path.cwd()
1245
+
1246
+ Return a new path object representing the current directory (as returned
1247
+ by :func: `os.getcwd `)::
1248
+
1249
+ >>> Path.cwd()
1250
+ PosixPath('/home/antoine/pathlib')
1251
+
1252
+
1253
+ .. classmethod :: Path.home()
1254
+
1255
+ Return a new path object representing the user's home directory (as
1256
+ returned by :func: `os.path.expanduser ` with ``~ `` construct). If the home
1257
+ directory can't be resolved, :exc: `RuntimeError ` is raised.
1258
+
1259
+ ::
1260
+
1261
+ >>> Path.home()
1262
+ PosixPath('/home/antoine')
1263
+
1264
+ .. versionadded :: 3.5
1265
+
1266
+
1267
+ .. method :: Path.chmod(mode, *, follow_symlinks=True)
1268
+
1269
+ Change the file mode and permissions, like :func: `os.chmod `.
1270
+
1271
+ This method normally follows symlinks. Some Unix flavours support changing
1272
+ permissions on the symlink itself; on these platforms you may add the
1273
+ argument ``follow_symlinks=False ``, or use :meth: `~Path.lchmod `.
1274
+
1275
+ ::
1276
+
1277
+ >>> p = Path('setup.py')
1278
+ >>> p.stat().st_mode
1279
+ 33277
1280
+ >>> p.chmod(0o444)
1281
+ >>> p.stat().st_mode
1282
+ 33060
1283
+
1284
+ .. versionchanged :: 3.10
1285
+ The *follow_symlinks * parameter was added.
1286
+
1287
+
1288
+ .. method :: Path.expanduser()
1289
+
1290
+ Return a new path with expanded ``~ `` and ``~user `` constructs,
1291
+ as returned by :meth: `os.path.expanduser `. If a home directory can't be
1292
+ resolved, :exc: `RuntimeError ` is raised.
1293
+
1294
+ ::
1295
+
1296
+ >>> p = PosixPath('~/films/Monty Python')
1297
+ >>> p.expanduser()
1298
+ PosixPath('/home/eric/films/Monty Python')
1299
+
1300
+ .. versionadded :: 3.5
1301
+
1302
+
1303
+ .. method :: Path.group()
1304
+
1305
+ Return the name of the group owning the file. :exc: `KeyError ` is raised
1306
+ if the file's gid isn't found in the system database.
1307
+
1308
+
1275
1309
.. method :: Path.lchmod(mode)
1276
1310
1277
1311
Like :meth: `Path.chmod ` but, if the path points to a symbolic link, the
@@ -1401,33 +1435,6 @@ example because the path doesn't exist).
1401
1435
.. versionchanged :: 3.6
1402
1436
The *strict * parameter was added (pre-3.6 behavior is strict).
1403
1437
1404
- .. method :: Path.rglob(pattern, *, case_sensitive=None)
1405
-
1406
- Glob the given relative *pattern * recursively. This is like calling
1407
- :func: `Path.glob ` with "``**/ ``" added in front of the *pattern *, where
1408
- *patterns * are the same as for :mod: `fnmatch `::
1409
-
1410
- >>> sorted(Path().rglob("*.py"))
1411
- [PosixPath('build/lib/pathlib.py'),
1412
- PosixPath('docs/conf.py'),
1413
- PosixPath('pathlib.py'),
1414
- PosixPath('setup.py'),
1415
- PosixPath('test_pathlib.py')]
1416
-
1417
- By default, or when the *case_sensitive * keyword-only argument is set to
1418
- ``None ``, this method matches paths using platform-specific casing rules:
1419
- typically, case-sensitive on POSIX, and case-insensitive on Windows.
1420
- Set *case_sensitive * to ``True `` or ``False `` to override this behaviour.
1421
-
1422
- .. audit-event :: pathlib.Path.rglob self,pattern pathlib.Path.rglob
1423
-
1424
- .. versionchanged :: 3.11
1425
- Return only directories if *pattern * ends with a pathname components
1426
- separator (:data: `~os.sep ` or :data: `~os.altsep `).
1427
-
1428
- .. versionchanged :: 3.12
1429
- The *case_sensitive * parameter was added.
1430
-
1431
1438
1432
1439
.. method :: Path.rmdir()
1433
1440
0 commit comments