본문 바로가기
파이썬

파이썬 IOError : [Errno 32] 깨진 파이프 : Python

by º기록 2021. 1. 29.
반응형

매우 간단한 Python 3 스크립트가 있습니다.

f1 = open('a.txt', 'r')
print(f1.readlines())
f2 = open('b.txt', 'r')
print(f2.readlines())
f3 = open('c.txt', 'r')
print(f3.readlines())
f4 = open('d.txt', 'r')
print(f4.readlines())
f1.close()
f2.close()
f3.close()
f4.close()

그러나 항상 다음과 같이 말합니다.

IOError: [Errno 32] Broken pipe

인터넷에서이 문제를 해결하는 모든 복잡한 방법을 보았지만이 코드를 직접 복사했기 때문에 Python의 SIGPIPE가 아니라 코드에 문제가 있다고 생각합니다.

출력을 리디렉션하고 있으므로 위 스크립트의 이름이 "open.py"인 경우 실행할 명령은 다음과 같습니다.

open.py | othercommand

 

해결 방법

 

문제를 재현하지는 못했지만이 방법으로 해결할 수 있습니다. ( print 를 사용하는 대신 stdout 에 한 줄씩 쓰기)

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

깨진 파이프를 잡을 수 있습니까? 파이프가 닫힐 때까지 파일을 stdout 에 한 줄씩 기록합니다.

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


 

참조 페이지 https://stackoverflow.com/questions/14207708

 

 

반응형

댓글