`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte` reading a log file.
IntermediateTroubleshootingRuntime, file, subprocess & encoding errors
Answer
Likely causes → The file is not UTF-8 (binary, UTF-16 with BOM 0xff 0xfe, Latin-1), is gzip-compressed, or contains a few corrupted bytes.
Diagnose →
file app.log
head -c 16 app.log | xxd
Fix → Use the right encoding (utf-16, latin-1), gzip.open(path, "rt") for compressed files, or errors="replace" for mostly-UTF-8 logs.
Prevent → Detect gzip by magic bytes or extension; standardize application logs on UTF-8.
More on Runtime, file, subprocess & encoding errors
`KeyError: 'Tags'` while processing `describe_instances` output.Beginner`TypeError: can't compare offset-naive and offset-aware datetimes` in a snapshot-age check.Beginner`UnicodeEncodeError: 'ascii' codec can't encode character '✓'` when printing in a container.Intermediate`PermissionError: [Errno 13] Permission denied: '/var/log/app/report.json'`Intermediate`OSError: [Errno 24] Too many open files` in a long-running Python service.Intermediate`subprocess.CalledProcessError: Command '['terraform', 'plan']' returned non-zero exit status 1.` with no further detail.Beginner