ShareYourGroupShareYourGroupError Explainer

Python UnicodeDecodeError: 'utf-8' codec can't decode byte

A plain-words explanation of this Python message: what it means, the usual causes, and the steps that fix it.

The messageUnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 3: unexpected end of data

Make the error yourself

Run with Python 3.14.10. This is the smallest example we could find that shows it.

Python program (main.py)

from pathlib import Path

Path("names.txt").write_bytes(b"caf\xe9")
print(Path("names.txt").read_text(encoding="utf-8"))

Output (the real message)

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(Path("names.txt").read_text(encoding="utf-8"))
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "<Python folder>\Lib\pathlib\__init__.py", line 788, in read_text
    return f.read()
           ~~~~~~^^
  File "<frozen codecs>", line 325, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 3: unexpected end of data

Why it happens

A file is a row of bytes; the encoding says which bytes mean which letters. You told Python to read the file as UTF-8, but the file was written in another encoding, so some bytes make no sense as UTF-8. The byte 0xe9 is "é" in the older Windows encoding cp1252 (and in latin-1), but in UTF-8 it is an incomplete start of a longer character. Files saved from Excel as "CSV" or from old Windows programs are very often cp1252. Indian-language text saved by some programs may be in yet another encoding.

How to fix it

  1. Try the likely encoding: open(path, encoding="cp1252"). On Windows many older files use it.
  2. For a quick look at a file that is mostly fine, errors="replace" replaces the bad bytes with a mark; use it to inspect, not to keep data.
  3. Better, fix the source: save the file as "UTF-8" in Excel (CSV UTF-8) or Notepad ("Save as" then Encoding).
  4. Always write the encoding explicitly when you open files, encoding="utf-8", so the program behaves the same on every computer.

The corrected version

Python program (main.py)

from pathlib import Path

Path("names.txt").write_bytes(b"caf\xe9")
print(Path("names.txt").read_text(encoding="cp1252"))

Output (the corrected version)

café

How to avoid it

Agree on UTF-8 for every file you create, and name the encoding in every open() call.

Questions

How do I find out the encoding of a file?

You cannot be sure, but the charset-normalizer package guesses well, and text editors such as Notepad++ show their guess.

This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.