Interview Prep/Python for DevOps/Runtime, file, subprocess & encoding errors

`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.

All Python for DevOps questions Roadmap: Python for automation

More on Runtime, file, subprocess & encoding errors