#!/usr/bin/env python import sys buffer = "" for i in range(100000): buffer += "%d\n" % i sys.stdout.write(buffer)
Pipe it into head and look at the output:
$ ./test.py | head -n1 0 ERROR:root:damn Traceback (most recent call last): File "./test.py", line 6, in <module> sys.stdout.write(buffer) IOError: [Errno 32] Broken pipe
I know there was a broken pipe and I don't care. Just swallow the worthless exception so I can see the meaningful output. This is probably the number one reason I often mutter "damn python crap" when using some of these tools. So if you are writing scripts in python, please be considerate and handle the broken pipe exception. Here is an example for quick reference:
#!/usr/bin/env python import errno import sys try: buffer = "" for i in range(100000): buffer += "%d\n" % i sys.stdout.write(buffer) except IOError, e: if e.errno != errno.EPIPE: raise e
No comments:
Post a Comment