[Python-checkins] cpython (3.5): #26001: mention in the tutorial that files in binary mode expect bytes, not str.

ezio.melotti python-checkins at python.org
Tue Jan 12 04:28:09 EST 2016


https://hg.python.org/cpython/rev/5a2692911a43
changeset:   99876:5a2692911a43
branch:      3.5
parent:      99874:84f34e8685de
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Tue Jan 12 11:27:30 2016 +0200
summary:
  #26001: mention in the tutorial that files in binary mode expect bytes, not str.

files:
  Doc/tutorial/inputoutput.rst |  15 ++++++++-------
  1 files changed, 8 insertions(+), 7 deletions(-)


diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst
--- a/Doc/tutorial/inputoutput.rst
+++ b/Doc/tutorial/inputoutput.rst
@@ -271,10 +271,11 @@
 ``f`` has already been created.
 
 To read a file's contents, call ``f.read(size)``, which reads some quantity of
-data and returns it as a string or bytes object.  *size* is an optional numeric
-argument.  When *size* is omitted or negative, the entire contents of the file
-will be read and returned; it's your problem if the file is twice as large as
-your machine's memory. Otherwise, at most *size* bytes are read and returned.
+data and returns it as a string (in text mode) or bytes object (in binary mode).
+*size* is an optional numeric argument.  When *size* is omitted or negative, the
+entire contents of the file will be read and returned; it's your problem if the
+file is twice as large as your machine's memory. Otherwise, at most *size* bytes
+are read and returned.
 If the end of the file has been reached, ``f.read()`` will return an empty
 string (``''``).  ::
 
@@ -315,11 +316,11 @@
    >>> f.write('This is a test\n')
    15
 
-To write something other than a string, it needs to be converted to a string
-first::
+Other types of objects need to be converted -- either to a string (in text mode)
+or a bytes object (in binary mode) -- before writing them::
 
    >>> value = ('the answer', 42)
-   >>> s = str(value)
+   >>> s = str(value)  # convert the tuple to string
    >>> f.write(s)
    18
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list