Other difference with Perl: Python scripts in a pipe

Ian Kelly ian.g.kelly at gmail.com
Thu Mar 10 17:16:04 EST 2016


On Thu, Mar 10, 2016 at 3:09 PM, Peter Otten <__peter__ at web.de> wrote:
> I suppose you need to fill the OS-level cache:
>
> $ cat somescript.py
> import sys
>
> for i in range(int(sys.argv[1])):
>     sys.stdout.write('line %d\n' % i)
> $ python somescript.py 20 | head -n5
> line 0
> line 1
> line 2
> line 3
> line 4
> $ python somescript.py 200 | head -n5
> line 0
> line 1
> line 2
> line 3
> line 4
> $ python somescript.py 2000 | head -n5
> line 0
> line 1
> line 2
> line 3
> line 4
> Traceback (most recent call last):
>   File "somescript.py", line 4, in <module>
>     sys.stdout.write('line %d\n' % i)
> IOError: [Errno 32] Broken pipe
>
> During my experiments I even got
>
> close failed in file object destructor:
> sys.excepthook is missing
> lost sys.stderr
>
> occasionally.

Interesting, both of these are probably worth bringing up as issues on
the bugs.python.org tracker. I'm not sure that the behavior should be
changed (if we get an error, we shouldn't just swallow it) but it does
seem like a significant hassle for writing command-line
text-processing tools.



More information about the Python-list mailing list