Other difference with Perl: Python scripts in a pipe

Fillmore fillmore_remove at hotmail.com
Thu Mar 10 17:04:38 EST 2016


On 3/10/2016 4:46 PM, Ian Kelly wrote:
> On Thu, Mar 10, 2016 at 2:33 PM, Fillmore <fillmore_remove at hotmail.com> wrote:
>>>> when I put a Python script in pipe with other commands, it will refuse to
>> let go silently. Any way I can avoid this?
>> What is your script doing? I don't see this problem.
>> ikelly at queso:~ $ cat somescript.py
> import sys
>> for i in range(20):
> sys.stdout.write('line %d\n' % i)

you are right. it's the with block :(
import sys
import csv
with open("somefile.tsv", newline='') as csvfile:
 myReader = csv.reader(csvfile, delimiter='\t')
 for row in myReader:
 for i in range(20):
 sys.stdout.write('line %d\n' % i)
$ ./somescript.py | head -5
line 0
line 1
line 2
line 3
line 4
Traceback (most recent call last):
 File "./somescript.py", line 12, in <module>
 sys.stdout.write('line %d\n' % i)
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' 
encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /