IOError: [Errno 32] 管道破裂:`prog.py

编程入门 行业动态 更新时间:2024-10-10 18:29:36
本文介绍了IOError: [Errno 32] 管道破裂:`prog.py |其他命令`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个非常简单的 Python 3 脚本:

I have a very simple Python 3 script:

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.

I saw on the internet all the complicated ways to fix this, but I copied this code directly, so I think that there is something wrong with the code and not Python's SIGPIPE.

我正在重定向输出,所以如果上面的脚本被命名为open.py",那么我要运行的命令是:

I am redirecting the output, so if the above script was named "open.py", then my command to run would be:

open.py | othercommand

推荐答案

我没有复现这个问题,但也许这个方法可以解决它:(将一行一行写入 stdout 而不是使用打印)

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)

你能抓住断掉的管道吗?这将文件逐行写入 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

您还需要确保 othercommand 在管道变得太大之前从管道中读取 - unix.stackexchange/questions/11946/how-big-is-the-pipe-buffer

You also need to make sure that othercommand is reading from the pipe before it gets too big - unix.stackexchange/questions/11946/how-big-is-the-pipe-buffer

更多推荐

IOError: [Errno 32] 管道破裂:`prog.py

本文发布于:2023-11-24 14:00:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管道   Errno   IOError   py   prog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!