@@ -708,19 +708,25 @@ available. They are listed here in alphabetical order.
708
708
for writing (truncating the file if it already exists), and ``'a' `` for
709
709
appending (which on *some * Unix systems means that *all * writes append to
710
710
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 *.
712
713
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 `.
718
721
719
722
.. index ::
720
723
single: line-buffered I/O
721
724
single: unbuffered I/O
722
725
single: buffer size, I/O
723
726
single: I/O control; buffering
727
+ single: binary mode
728
+ single: text mode
729
+ module: sys
724
730
725
731
The optional *bufsize * argument specifies the file's desired buffer size:
726
732
0 means unbuffered, 1 means line buffered, any other positive value means
@@ -730,28 +736,20 @@ available. They are listed here in alphabetical order.
730
736
used. [# ]_
731
737
732
738
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' ``,
748
749
``'\r' ``, ``'\r\n' ``, or a tuple containing all the newline types seen.
749
750
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.
755
753
756
754
757
755
.. function :: ord(c)
0 commit comments