Location
Chapter 3, section “Files and the Operating System”, in ch03.ipynb.
The relevant cells are currently In [110] and In [112].
Current example
First, the text file is opened without specifying an encoding:
f1 = open(path)
f1.read(10)
f2 = open(path, mode="rb") # Binary mode
f2.read(10)
After comparing the text and binary file positions, the chapter explains that the additional byte is related to decoding the characters with the default encoding. It then shows:
import sys
sys.getdefaultencoding()
Why this may be misleading
sys.getdefaultencoding() reports Python's default string encoding. It does not necessarily report the encoding being used by a text file opened with open().
When encoding is omitted, open() uses a platform-dependent locale encoding. Therefore, these values can be different. For example, on a Windows environment:
>>> sys.getdefaultencoding()
'utf-8'
>>> f1.encoding
'cp1252'
This distinction matters here because examples/segismundo.txt is UTF-8 encoded and contains non-ASCII characters such as ñ. On a platform whose locale encoding is not UTF-8, f1 = open(path) may decode the file differently even though sys.getdefaultencoding() still returns "utf-8".
References:
Suggested improvement
Since f1 is already open, the example could inspect the encoding actually used by that file object:
The accompanying sentence could also be changed to something like:
The encoding selected for this text file can be inspected through the file object's encoding attribute:
This would preserve the purpose of the example while accurately showing the encoding used to decode f1 and making the platform-dependent behavior clearer to readers.
Location
Chapter 3, section “Files and the Operating System”, in
ch03.ipynb.The relevant cells are currently
In [110]andIn [112].Current example
First, the text file is opened without specifying an encoding:
After comparing the text and binary file positions, the chapter explains that the additional byte is related to decoding the characters with the default encoding. It then shows:
Why this may be misleading
sys.getdefaultencoding()reports Python's default string encoding. It does not necessarily report the encoding being used by a text file opened withopen().When
encodingis omitted,open()uses a platform-dependent locale encoding. Therefore, these values can be different. For example, on a Windows environment:This distinction matters here because
examples/segismundo.txtis UTF-8 encoded and contains non-ASCII characters such asñ. On a platform whose locale encoding is not UTF-8,f1 = open(path)may decode the file differently even thoughsys.getdefaultencoding()still returns"utf-8".References:
open()documentation: when no encoding is specified in text mode, the encoding is platform-dependent.sys.getdefaultencoding()documentation: returns the default string encoding used by the Unicode implementation.Suggested improvement
Since
f1is already open, the example could inspect the encoding actually used by that file object:The accompanying sentence could also be changed to something like:
This would preserve the purpose of the example while accurately showing the encoding used to decode
f1and making the platform-dependent behavior clearer to readers.