Your script ends with `BrokenPipeError: [Errno 32] Broken pipe` when piped to `head`.
AdvancedTroubleshootingRuntime, file, subprocess & encoding errors
Answer
Likely causes → head exits after reading enough lines and closes the pipe; Python's next write to stdout fails.
Diagnose → python report.py | head -5 reproduces it.
Fix → Handle it at the entry point:
try:
main()
except BrokenPipeError:
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1)
The Python docs recommend this pattern.
Prevent → Include it in your CLI template; click already handles many cases.
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`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte` reading a log file.Intermediate`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