prize18-7 Exercise 1: Read a File of Unknown Encoding ====================================================== Plan: strict UTF-8 (via utf-8-sig, which also removes a BOM) is a strong test, because most non-UTF-8 text with accents is not valid UTF-8. If it fails, fall back to Windows-1252, the most common legacy encoding for Western European text. ex1.py: import pathlib, tempfile def read_guess(path): raw = pathlib.Path(path).read_bytes() try: return raw.decode("utf-8-sig"), "utf-8" except UnicodeDecodeError: return raw.decode("cp1252", errors="replace"), "cp1252 (fallback)" d = pathlib.Path(tempfile.mkdtemp()) samples = { "plain utf-8": "caf\u00e9".encode("utf-8"), "utf-8 with BOM": "caf\u00e9".encode("utf-8-sig"), "cp1252": "caf\u00e9 \u20ac".encode("cp1252"), "ascii only": b"hello", } for name, raw in samples.items(): p = d / (name.replace(" ", "_") + ".txt") p.write_bytes(raw) text, enc = read_guess(p) print(f"{name:15} -> {enc:18} {text!r}") Real output (Python 3.14): plain utf-8 -> utf-8 'café' utf-8 with BOM -> utf-8 'café' cp1252 -> cp1252 (fallback) 'café €' ascii only -> utf-8 'hello' Weakness: it is a guess. A short Windows-1252 file may happen to be valid UTF-8 and be misread (rare for real text but possible), and a file in any other legacy encoding, such as a Cyrillic one, would be decoded as Windows-1252 without any error and come out as wrong characters. Also, with errors="replace" the fallback hides bad bytes. Where you control the writer, declare the encoding instead of guessing.