PlainError

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Last checked 2026-10-31 · 4 fixes · reviewed answer

TL;DRThis Python error happens when code tries to read text from a file using UTF-8 encoding, but the file starts with a byte (0xff) that UTF-8 does not allow at the beginning. UTF-8 is a standard way computers turn bytes into readable characters; byte 0xff often appears in binary files, images, or text saved in other encodings like UTF-16 or Windows-1252. The program stops because it cannot turn that byte into text.

Most likely cause: The file is UTF-16 text (it starts with byte 0xFF 0xFE). Windows PowerShell 5.1 creates these with > or Out-File, and some Windows tools export them

Try first: Look at the first bytes (python -c "print(open('yourfile','rb').read(4))")

Likely causes

  1. The file is UTF-16 text (it starts with byte 0xFF 0xFE). Windows PowerShell 5.1 creates these with > or Out-File, and some Windows tools export themmost likely
  2. The file is binary (an image, PDF, zip or executable) but it was opened in text modepossible
  3. The file uses another encoding or is corruptedless likely

How to fix it

Safest first: SafeChanges settingsDangerous
  1. Look at the first bytesSafe

    Print the first bytes of the file: b'\xff\xfe' means UTF-16; something like b'\xff\xd8' is a JPEG image.

    python -c "print(open('yourfile','rb').read(4))"
  2. Open UTF-16 files with the right encodingChanges settings

    If the file starts with b'\xff\xfe', open it with encoding='utf-16'.

    open('yourfile', encoding='utf-16')
  3. Open binary files in binary modeChanges settings

    For images, PDFs and other binary files, use 'rb' and work with the bytes instead of text.

    open('yourfile', 'rb')
  4. Save the file as UTF-8 next timeChanges settings

    In PowerShell use Out-File -Encoding utf8 (or PowerShell 7, which defaults to UTF-8), or re-save the file as UTF-8 in your editor.

If that didn't fix it

Print the first 20 bytes of the file with open('file','rb').read(20) to see the actual starting bytes and compare them to known file signatures.

When to see a technician

Consult a developer or post the surrounding code and the exact file type if the error persists after trying different encodings.

Quick answers

What does UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte mean?
This Python error happens when code tries to read text from a file using UTF-8 encoding, but the file starts with a byte (0xff) that UTF-8 does not allow at the beginning. UTF-8 is a standard way computers turn bytes into readable characters; byte 0xff often appears in binary files, images, or text saved in other encodings like UTF-16 or Windows-1252. The program stops because it cannot turn that byte into text.
What causes UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte?
The file is UTF-16 text (it starts with byte 0xFF 0xFE). Windows PowerShell 5.1 creates these with > or Out-File, and some Windows tools export them. (most likely) The file is binary (an image, PDF, zip or executable) but it was opened in text mode. (possible) The file uses another encoding or is corrupted. (less likely)
How do I fix UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte?
Try these in order, safest first: 1. Look at the first bytes. 2. Open UTF-16 files with the right encoding. 3. Open binary files in binary mode. 4. Save the file as UTF-8 next time.
What if that didn't fix it?
Print the first 20 bytes of the file with open('file','rb').read(20) to see the actual starting bytes and compare them to known file signatures.
When should I see a technician?
Consult a developer or post the surrounding code and the exact file type if the error persists after trying different encodings.
Did this fix it? Report a problem

Related errors

All Coding errors →