https://github.com/python/cpython/commit/a8e3247206e5b9e50284565ccaef659097a96a88 commit: a8e3247206e5b9e50284565ccaef659097a96a88 branch: main author: Omkar Kabde <[email protected]> committer: nedbat <[email protected]> date: 2026-05-10T17:41:37Z summary:
gh-149611: Explain return values for `Path.write_text()` and `Path.write_bytes()` (#149629) specify return explanation Co-authored-by: Ned Batchelder <[email protected]> files: M Doc/library/pathlib.rst M Lib/pathlib/__init__.py M Lib/pathlib/types.py diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 2867015042ee16..923cd4a2c4c1f1 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -1266,6 +1266,8 @@ Reading and writing files >>> p.read_text() 'Text file contents' + Return the number of characters written. + An existing file of the same name is overwritten. The optional parameters have the same meaning as in :func:`open`. @@ -1286,6 +1288,8 @@ Reading and writing files >>> p.read_bytes() b'Binary file contents' + Return the number of bytes written. + An existing file of the same name is overwritten. .. versionadded:: 3.5 diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index a32e4b5320ff6d..295f633824a6ef 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -989,6 +989,7 @@ def read_text(self, encoding=None, errors=None, newline=None): def write_bytes(self, data): """ Open the file in bytes mode, write to it, and close the file. + Return the number of bytes written. """ # type-check for the buffer interface before truncating the file view = memoryview(data) @@ -998,6 +999,7 @@ def write_bytes(self, data): def write_text(self, data, encoding=None, errors=None, newline=None): """ Open the file in text mode, write to it, and close the file. + Return the number of characters written. """ # Call io.text_encoding() here to ensure any warning is raised at an # appropriate stack level. diff --git a/Lib/pathlib/types.py b/Lib/pathlib/types.py index f21ce0774548f8..bb4a521223da04 100644 --- a/Lib/pathlib/types.py +++ b/Lib/pathlib/types.py @@ -431,6 +431,7 @@ def __open_writer__(self, mode): def write_bytes(self, data): """ Open the file in bytes mode, write to it, and close the file. + Return the number of bytes written. """ # type-check for the buffer interface before truncating the file view = memoryview(data) @@ -440,6 +441,7 @@ def write_bytes(self, data): def write_text(self, data, encoding=None, errors=None, newline=None): """ Open the file in text mode, write to it, and close the file. + Return the number of characters written. """ # Call io.text_encoding() here to ensure any warning is raised at an # appropriate stack level. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
