@@ -1050,6 +1050,90 @@ Querying file type and status
10501050 .. versionadded :: 3.5
10511051
10521052
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+
10531137Other methods
10541138^^^^^^^^^^^^^
10551139
@@ -1343,18 +1427,6 @@ example because the path doesn't exist).
13431427 The *exist_ok * parameter was added.
13441428
13451429
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-
13581430.. method :: Path.owner(*, follow_symlinks=True)
13591431
13601432 Return the name of the user owning the file. :exc: `KeyError ` is raised
@@ -1371,37 +1443,6 @@ example because the path doesn't exist).
13711443 The *follow_symlinks * parameter was added.
13721444
13731445
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-
14051446.. method :: Path.readlink()
14061447
14071448 Return the path to which the symbolic link points (as returned by
@@ -1576,42 +1617,6 @@ example because the path doesn't exist).
15761617 The *missing_ok * parameter was added.
15771618
15781619
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-
16151620.. _pathlib-pattern-language :
16161621
16171622Pattern language
0 commit comments