close

[Solved] IOError: [Errno 32] Broken pipe when piping: `prog.py | othercmd`

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd Error Occurs?

Today I get the following error IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd in python.

How To Solve IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd Error ?

  1. How To Solve IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd Error ?

    To Solve IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd Error You could catch the broken pipe? This writes the file to stdout line by line until the pipe is closed.

  2. IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd

    To Solve IOError: [Errno 32] Broken pipe when piping: prog.py | othercmd Error You could catch the broken pipe? This writes the file to stdout line by line until the pipe is closed.

Solution 1

I haven’t reproduced the issue, but perhaps this method would solve it: (writing line by line to stdout rather than using print)

import sys
with open('a.txt', 'r') as f1:
    for line in f1:
        sys.stdout.write(line)

You could catch the broken pipe? This writes the file to stdout line by line until the pipe is closed.

import sys, errno
try:
    with open('a.txt', 'r') as f1:
        for line in f1:
            sys.stdout.write(line)
except IOError as e:
    if e.errno == errno.EPIPE:
        # Handle error

Solution 2

The problem is due to SIGPIPE handling. You can solve this problem using the following code:

from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL) 

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read