Skip to content

Commit 7b6ca4a

Browse files
committed
python#5061: improve open() docs a bit.
1 parent 47a7d70 commit 7b6ca4a

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Doc/library/functions.rst

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -676,38 +676,39 @@ are always available. They are listed here in alphabetical order.
676676
*closefd* is set to ``False``.)
677677

678678
*mode* is an optional string that specifies the mode in which the file is
679-
opened. It defaults to ``'r'`` which means open for reading in text mode.
680-
Other common values are ``'w'`` for writing (truncating the file if it
681-
already exists), and ``'a'`` for appending (which on *some* Unix systems,
682-
means that *all* writes append to the end of the file regardless of the
683-
current seek position). In text mode, if *encoding* is not specified the
684-
encoding used is platform dependent. (For reading and writing raw bytes use
685-
binary mode and leave *encoding* unspecified.) The available modes are:
679+
opened. The available modes are:
686680

687681
========= ===============================================================
688682
Character Meaning
689683
--------- ---------------------------------------------------------------
690684
``'r'`` open for reading (default)
691-
``'w'`` open for writing, truncating the file first
685+
``'w'`` open for writing, truncating the file first if it exists
692686
``'a'`` open for writing, appending to the end of the file if it exists
693-
``'b'`` binary mode
687+
========= ===============================================================
688+
689+
Several characters can be appended that modify the given mode:
690+
691+
========= ===============================================================
694692
``'t'`` text mode (default)
695-
``'+'`` open a disk file for updating (reading and writing)
693+
``'b'`` binary mode
694+
``'+'`` open for updating (reading and writing)
696695
``'U'`` universal newline mode (for backwards compatibility; should
697696
not be used in new code)
698697
========= ===============================================================
699698

700-
The default mode is ``'rt'`` (open for reading text). For binary random
701-
access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
702-
``'r+b'`` opens the file without truncation.
699+
The mode ``'w+'`` opens and truncates the file to 0 bytes, while ``'r+'``
700+
opens the file without truncation. On *some* Unix systems, append mode means
701+
that *all* writes append to the end of the file regardless of the current
702+
seek position.
703703

704704
Python distinguishes between files opened in binary and text modes, even when
705705
the underlying operating system doesn't. Files opened in binary mode
706706
(including ``'b'`` in the *mode* argument) return contents as ``bytes``
707707
objects without any decoding. In text mode (the default, or when ``'t'`` is
708708
included in the *mode* argument), the contents of the file are returned as
709-
strings, the bytes having been first decoded using a platform-dependent
710-
encoding or using the specified *encoding* if given.
709+
strings, the bytes having been first decoded using the specified *encoding*.
710+
If *encoding* is not specified, a platform-dependent default encoding is
711+
used, see below.
711712

712713
*buffering* is an optional integer used to set the buffering policy. By
713714
default full buffering is on. Pass 0 to switch buffering off (only allowed

0 commit comments

Comments
 (0)