Skip to content

Chapter 3: use f1.encoding instead of sys.getdefaultencoding() when explaining open() #224

Description

@FranDSchz

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:

f1.encoding

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:

f1.encoding

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions