Skip to content

Commit a8e3247

Browse files
omkar-334nedbat
andauthored
gh-149611: Explain return values for Path.write_text() and Path.write_bytes() (#149629)
specify return explanation Co-authored-by: Ned Batchelder <ned@nedbatchelder.com>
1 parent 24b8f12 commit a8e3247

3 files changed

Lines changed: 8 additions & 0 deletions

File tree

Doc/library/pathlib.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,8 @@ Reading and writing files
12661266
>>> p.read_text()
12671267
'Text file contents'
12681268

1269+
Return the number of characters written.
1270+
12691271
An existing file of the same name is overwritten. The optional parameters
12701272
have the same meaning as in :func:`open`.
12711273

@@ -1286,6 +1288,8 @@ Reading and writing files
12861288
>>> p.read_bytes()
12871289
b'Binary file contents'
12881290

1291+
Return the number of bytes written.
1292+
12891293
An existing file of the same name is overwritten.
12901294

12911295
.. versionadded:: 3.5

Lib/pathlib/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ def read_text(self, encoding=None, errors=None, newline=None):
989989
def write_bytes(self, data):
990990
"""
991991
Open the file in bytes mode, write to it, and close the file.
992+
Return the number of bytes written.
992993
"""
993994
# type-check for the buffer interface before truncating the file
994995
view = memoryview(data)
@@ -998,6 +999,7 @@ def write_bytes(self, data):
998999
def write_text(self, data, encoding=None, errors=None, newline=None):
9991000
"""
10001001
Open the file in text mode, write to it, and close the file.
1002+
Return the number of characters written.
10011003
"""
10021004
# Call io.text_encoding() here to ensure any warning is raised at an
10031005
# appropriate stack level.

Lib/pathlib/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ def __open_writer__(self, mode):
431431
def write_bytes(self, data):
432432
"""
433433
Open the file in bytes mode, write to it, and close the file.
434+
Return the number of bytes written.
434435
"""
435436
# type-check for the buffer interface before truncating the file
436437
view = memoryview(data)
@@ -440,6 +441,7 @@ def write_bytes(self, data):
440441
def write_text(self, data, encoding=None, errors=None, newline=None):
441442
"""
442443
Open the file in text mode, write to it, and close the file.
444+
Return the number of characters written.
443445
"""
444446
# Call io.text_encoding() here to ensure any warning is raised at an
445447
# appropriate stack level.

0 commit comments

Comments
 (0)