I've written something to convert html table to csv and it has a part like this:
out_csv = csv.writer(sys.stdout)
for row in table:
out_csv.writerow(row.to_csv())
It works just fine until I pipe into a program that doesn't use the full output, i.e:
python file.py | head -5
. Now the file closes after the 5th line and I get a BrokenPipeError.
I know I could put the whole code into a try and except block, but from what I gather its bad practice and it isn't very readable either.
Is there any other way I could deal with this?