Problem Description
When piping output to head, the program exits with BrokenPipeError.
Proposed solution
The python docs explain how to handle this situation, and it is quite an easy fix. https://docs.python.org/3/library/signal.html#note-on-sigpipe