我有一个非常简单的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()
Run Code Online (Sandbox Code Playgroud)
但它总是说:
IOError: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
我在互联网上看到了解决这个问题的所有复杂方法,但是我直接复制了这个代码,所以我认为代码有问题而不是Python的SIGPIPE.
我正在重定向输出,所以如果上面的脚本命名为"open.py",那么我的运行命令是:
open.py | othercommand
Run Code Online (Sandbox Code Playgroud) 我正在尝试打印在我的格式化的元组列表stdout
.为此,我使用str.format方法.一切正常,但是当我输出输出以使用head
命令a 查看第一行时IOError
.
这是我的代码:
# creating the data
data = []$
for i in range(0, 1000):
pid = 'pid%d' % i
uid = 'uid%d' % i
pname = 'pname%d' % i
data.append( (pid, uid, pname) )
# find max leghed string for each field
pids, uids, pnames = zip(*data)
max_pid = len("%s" % max( pids) )
max_uid = len("%s" % max( uids) )
max_pname = len("%s" % max( pnames) )
# my template …
Run Code Online (Sandbox Code Playgroud)