Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e02c50

Browse files
author
Skip Montanaro
committed
Clarify the difference between text and binary files. I'm not sure the
tutorial is the right place to mention a file object's encoding.
1 parent 5dde61d commit 4e02c50

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

Doc/tutorial/inputoutput.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,20 @@ automatically added to the end. ``'r+'`` opens the file for both reading and
197197
writing. The *mode* argument is optional; ``'r'`` will be assumed if it's
198198
omitted.
199199

200-
On Windows and the Macintosh, ``'b'`` appended to the mode opens the file in
201-
binary mode, so there are also modes like ``'rb'``, ``'wb'``, and ``'r+b'``.
202-
Windows makes a distinction between text and binary files; the end-of-line
203-
characters in text files are automatically altered slightly when data is read or
204-
written. This behind-the-scenes modification to file data is fine for ASCII
205-
text files, but it'll corrupt binary data like that in :file:`JPEG` or
206-
:file:`EXE` files. Be very careful to use binary mode when reading and writing
207-
such files.
200+
``'b'`` appended to the mode opens the file in binary mode, so there are
201+
also modes like ``'rb'``, ``'wb'``, and ``'r+b'``. Python distinguishes
202+
between text and binary files. Binary files are read and written without
203+
any data transformation. In text mode, platform-specific newline
204+
representations are automatically converted to newlines when read and
205+
newline characters are automatically converted to the proper
206+
platform-specific representation when written. This makes writing portable
207+
code which reads or writes text files easier. In addition, when reading
208+
from or writing to text files, the data are automatically decoded or
209+
encoding, respectively, using the encoding associated with the file.
210+
211+
This behind-the-scenes modification to file data is fine for text files, but
212+
will corrupt binary data like that in :file:`JPEG` or :file:`EXE` files. Be
213+
very careful to use binary mode when reading and writing such files.
208214

209215

210216
.. _tut-filemethods:

0 commit comments

Comments
 (0)