@@ -1050,6 +1050,90 @@ Querying file type and status
1050
1050
.. versionadded :: 3.5
1051
1051
1052
1052
1053
+ Reading and writing files
1054
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
1055
+
1056
+
1057
+ .. method :: Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
1058
+
1059
+ Open the file pointed to by the path, like the built-in :func: `open `
1060
+ function does::
1061
+
1062
+ >>> p = Path('setup.py')
1063
+ >>> with p.open() as f:
1064
+ ... f.readline()
1065
+ ...
1066
+ '#!/usr/bin/env python3\n'
1067
+
1068
+
1069
+ .. method :: Path.read_text(encoding=None, errors=None, newline=None)
1070
+
1071
+ Return the decoded contents of the pointed-to file as a string::
1072
+
1073
+ >>> p = Path('my_text_file')
1074
+ >>> p.write_text('Text file contents')
1075
+ 18
1076
+ >>> p.read_text()
1077
+ 'Text file contents'
1078
+
1079
+ The file is opened and then closed. The optional parameters have the same
1080
+ meaning as in :func: `open `.
1081
+
1082
+ .. versionadded :: 3.5
1083
+
1084
+ .. versionchanged :: 3.13
1085
+ The *newline * parameter was added.
1086
+
1087
+
1088
+ .. method :: Path.read_bytes()
1089
+
1090
+ Return the binary contents of the pointed-to file as a bytes object::
1091
+
1092
+ >>> p = Path('my_binary_file')
1093
+ >>> p.write_bytes(b'Binary file contents')
1094
+ 20
1095
+ >>> p.read_bytes()
1096
+ b'Binary file contents'
1097
+
1098
+ .. versionadded :: 3.5
1099
+
1100
+
1101
+ .. method :: Path.write_text(data, encoding=None, errors=None, newline=None)
1102
+
1103
+ Open the file pointed to in text mode, write *data * to it, and close the
1104
+ file::
1105
+
1106
+ >>> p = Path('my_text_file')
1107
+ >>> p.write_text('Text file contents')
1108
+ 18
1109
+ >>> p.read_text()
1110
+ 'Text file contents'
1111
+
1112
+ An existing file of the same name is overwritten. The optional parameters
1113
+ have the same meaning as in :func: `open `.
1114
+
1115
+ .. versionadded :: 3.5
1116
+
1117
+ .. versionchanged :: 3.10
1118
+ The *newline * parameter was added.
1119
+
1120
+
1121
+ .. method :: Path.write_bytes(data)
1122
+
1123
+ Open the file pointed to in bytes mode, write *data * to it, and close the
1124
+ file::
1125
+
1126
+ >>> p = Path('my_binary_file')
1127
+ >>> p.write_bytes(b'Binary file contents')
1128
+ 20
1129
+ >>> p.read_bytes()
1130
+ b'Binary file contents'
1131
+
1132
+ An existing file of the same name is overwritten.
1133
+
1134
+ .. versionadded :: 3.5
1135
+
1136
+
1053
1137
Other methods
1054
1138
^^^^^^^^^^^^^
1055
1139
@@ -1343,18 +1427,6 @@ example because the path doesn't exist).
1343
1427
The *exist_ok * parameter was added.
1344
1428
1345
1429
1346
- .. method :: Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
1347
-
1348
- Open the file pointed to by the path, like the built-in :func: `open `
1349
- function does::
1350
-
1351
- >>> p = Path('setup.py')
1352
- >>> with p.open() as f:
1353
- ... f.readline()
1354
- ...
1355
- '#!/usr/bin/env python3\n'
1356
-
1357
-
1358
1430
.. method :: Path.owner(*, follow_symlinks=True)
1359
1431
1360
1432
Return the name of the user owning the file. :exc: `KeyError ` is raised
@@ -1371,37 +1443,6 @@ example because the path doesn't exist).
1371
1443
The *follow_symlinks * parameter was added.
1372
1444
1373
1445
1374
- .. method :: Path.read_bytes()
1375
-
1376
- Return the binary contents of the pointed-to file as a bytes object::
1377
-
1378
- >>> p = Path('my_binary_file')
1379
- >>> p.write_bytes(b'Binary file contents')
1380
- 20
1381
- >>> p.read_bytes()
1382
- b'Binary file contents'
1383
-
1384
- .. versionadded :: 3.5
1385
-
1386
-
1387
- .. method :: Path.read_text(encoding=None, errors=None, newline=None)
1388
-
1389
- Return the decoded contents of the pointed-to file as a string::
1390
-
1391
- >>> p = Path('my_text_file')
1392
- >>> p.write_text('Text file contents')
1393
- 18
1394
- >>> p.read_text()
1395
- 'Text file contents'
1396
-
1397
- The file is opened and then closed. The optional parameters have the same
1398
- meaning as in :func: `open `.
1399
-
1400
- .. versionadded :: 3.5
1401
-
1402
- .. versionchanged :: 3.13
1403
- The *newline * parameter was added.
1404
-
1405
1446
.. method :: Path.readlink()
1406
1447
1407
1448
Return the path to which the symbolic link points (as returned by
@@ -1576,42 +1617,6 @@ example because the path doesn't exist).
1576
1617
The *missing_ok * parameter was added.
1577
1618
1578
1619
1579
- .. method :: Path.write_bytes(data)
1580
-
1581
- Open the file pointed to in bytes mode, write *data * to it, and close the
1582
- file::
1583
-
1584
- >>> p = Path('my_binary_file')
1585
- >>> p.write_bytes(b'Binary file contents')
1586
- 20
1587
- >>> p.read_bytes()
1588
- b'Binary file contents'
1589
-
1590
- An existing file of the same name is overwritten.
1591
-
1592
- .. versionadded :: 3.5
1593
-
1594
-
1595
- .. method :: Path.write_text(data, encoding=None, errors=None, newline=None)
1596
-
1597
- Open the file pointed to in text mode, write *data * to it, and close the
1598
- file::
1599
-
1600
- >>> p = Path('my_text_file')
1601
- >>> p.write_text('Text file contents')
1602
- 18
1603
- >>> p.read_text()
1604
- 'Text file contents'
1605
-
1606
- An existing file of the same name is overwritten. The optional parameters
1607
- have the same meaning as in :func: `open `.
1608
-
1609
- .. versionadded :: 3.5
1610
-
1611
- .. versionchanged :: 3.10
1612
- The *newline * parameter was added.
1613
-
1614
-
1615
1620
.. _pathlib-pattern-language :
1616
1621
1617
1622
Pattern language
0 commit comments