0

Say I have this code:

for i in range(200):
    print("\n" * 200)

and I run it like this:

$ python oops.py | less

then quit less before scrolling down, Python throws an exception:

Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp1252'>
OSError: [Errno 22] Invalid argument

How do I handle this cleanly? Ideally I want to behave like cat does and silently stop, however the exception doesn't appear to have any information in it other than "invalid argument".

Lexi
  • 1,670
  • 1
  • 19
  • 35
  • Side note: I don't know why the exception is being ignored in my example, but in my main program it has a full stack trace – Lexi Dec 22 '20 at 12:24
  • 1
    Look at the second highest voted answer in the link @0stone0 shared. – Ted Lyngmo Dec 22 '20 at 12:35

0 Answers0