python foo.py| head
and you get a Broken Pipe Error then the next code snippet could be useful.
import sys
for line in sys.stdin:
print line.strip()
Then run your program and you will get
$ yes | python pipe.py |head
y
y
y
y
y
Traceback (most recent call last):
File "pipe.py", line 5, in ?
print line
IOError: [Errno 32] Broken pipe
To avoid this error just use signal package:
import signal
import sys
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
for line in sys.stdin:
print line.strip()
Enjoy
Thanks for that code snippet. Works like a charm. Now I just need to read up about *why* it works. :-)
ResponderEliminar