Reputation: 25
import sys
input_1 = sys.argv[1]
in_1 = open(input_1, 'r')
input_lines = in_1.readlines()
in_1.close()
length = len(input_lines)
for line in range(3,length):
print(line,input_lines[line], end="")
exit()
I am getting BrokenPipeError: [Errno 32].
Upvotes: 1
Views: 8121
Reputation: 5502
There is no issue with the code you posted. Something else is going on here. Are you piping to some other program that is closing the pipe?
e.g. script.py input.txt | head
or script.py input.txt | tail
If so then just store the output in a file or variable first. For example:
script.py input.txt > output.txt
head output.txt
Or...
output=$(script.py input.txt)
head <<<"$output"
If you just want to suppress the error, you can replace the end of your script with the following:
try:
for line in range(3, length):
print(line, input_lines[line], end="")
except BrokenPipeError:
pass
Upvotes: 2