File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -98,7 +98,17 @@ function it uses to do this is available:
9898 but disagree, a SyntaxError will be raised. Note that if the BOM is found,
9999 ``'utf-8-sig' `` will be returned as an encoding.
100100
101- If no encoding is specified, then the default of ``'utf-8' `` will be returned.
101+ If no encoding is specified, then the default of ``'utf-8' `` will be
102+ returned.
103+
104+ :func: `detect_encoding ` is useful for robustly reading Python source files.
105+ A common pattern for this follows::
106+
107+ def read_python_source(file_name):
108+ with open(file_name, "rb") as fp:
109+ encoding = tokenize.detect_encoding(fp.readline)[0]
110+ with open(file_name, "r", encoding=encoding) as fp:
111+ return fp.read()
102112
103113
104114Example of a script re-writer that transforms float literals into Decimal
You can’t perform that action at this time.
0 commit comments