8000 Update the documentation of the open() builtin function a bit. I bel… · python/cpython@4d8c193 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d8c193

Browse files
author
Skip Montanaro
committed
Update the documentation of the open() builtin function a bit. I believe I
mostly got the distinction between text and binary modes correct, though someone should proofread my writing. I also sort of guessed at the meaning of the various index:: entries.
1 parent 1c63960 commit 4d8c193

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

Doc/library/functions.rst

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -708,19 +708,25 @@ available. They are listed here in alphabetical order.
708708
for writing (truncating the file if it already exists), and ``'a'`` for
709709
appending (which on *some* Unix systems means that *all* writes append to
710710
the end of the file regardless of the current seek position). If *mode*
711-
is omitted, it defaults to ``'r'``.
711+
is omitted, it defaults to ``'r'``. See below for more possible values
712+
of *mode*.
712713

713-
When opening a binary file, you should append ``'b'`` to the *mode* value
714-
to open the file in binary mode, which will improve portability.
715-
(Appending ``'b'`` is useful even on systems that don't treat binary and
716-
text files differently, where it serves as documentation.) See below for
717-
more possible values of *mode*.
714+
Python distinguishes between files opened in binary and text modes, even
715+
when the underlying operating system doesn't. Files opened in binary
716+
mode (appending ``'b'`` to the *mode* argument to :func:``open``) return
717+
contents as bytes objects without any decoding. In text mode (the
718+
default, or when ``'t'`` is appended to the *mode* argument) the contents
719+
of the file are returned as strings, the bytes having been first decoded
720+
using the encoding specified by :func:`sys.getfilesystemencoding`.
718721

719722
.. index::
720723
single: line-buffered I/O
721724
single: unbuffered I/O
722725
single: buffer size, I/O
723726
single: I/O control; buffering
727+
single: binary mode
728+
single: text mode
729+
module: sys
724730

725731
The optional *bufsize* argument specifies the file's desired buffer size:
726732
0 means unbuffered, 1 means line buffered, any other positive value means
@@ -730,28 +736,20 @@ available. They are listed here in alphabetical order.
730736
used. [#]_
731737

732738
Modes ``'r+'``, ``'w+'`` and ``'a+'`` open the file for updating (note
733-
that ``'w+'`` truncates the file). Append ``'b'`` to the mode to open
734-
the file in binary mode, on systems that differentiate between binary and
735-
text files; on systems that don't have this distinction, adding the
736-
``'b'`` has no effect.
737-
738-
In addition to the standard :cfunc:`fopen` values *mode* may be ``'U'``
739-
or ``'rU'``. Python is usually built with universal newline support;
740-
supplying ``'U'`` opens the file as a text file, but lines may be
741-
terminated by any of the following: the Unix end-of-line convention
742-
``'\n'``, the Macintosh convention ``'\r'``, or the Windows convention
743-
``'\r\n'``. All of these external representations are seen as ``'\n'`` by
744-
the Python program. If Python is built without universal newline support
745-
a *mode* with ``'U'`` is the same as normal text mode. Note that file
746-
objects so opened also have an attribute called :attr:`newlines` which
747-
has a value of ``None`` (if no newlines have yet been seen), ``'\n'``,
739+
that ``'w+'`` truncates the file).
740+
741+
When a file is opened in text mode it is also opened in universal
742+
newlines mode. Unlike earlier versions of Python it's no longer
743+
necessary to add a ``'U'`` value to the *mode* argument to enable this
744+
mode. Consequently, in files opened in text mode lines may be terminated
745+
with ``'\n'``, ``'\r'``, or ``'\r\n'``. All three external
746+
representations are seen as ``'\n'`` by the Python program. File objects
747+
opened in text mode also have a :attr:`newlines` attribute which has a
748+
value of ``None`` (if no newlines have been seen yet), ``'\n'``,
748749
``'\r'``, ``'\r\n'``, or a tuple containing all the newline types seen.
749750

750-
Python enforces that the mode, after stripping ``'U'``, begins with
751-
``'r'``, ``'w'`` or ``'a'``.
752-
753-
See also the :mod:`fileinput` module, the :mod:`os` module, and the
754-
:mod:`os.path` module.
751+
See also the :mod:`fileinput` module, the file-related functions in the
752+
:mod:`os` module, and the :mod:`os.path` module.
755753

756754

757755
.. function:: ord(c)

0 commit comments

Comments
 (0)
0