带有IOError的python打印:[Errno 0]错误

时间:2018-04-06 07:47:44

标签: python-2.7 printing ioerror

ll=[]
for ii in range(26):
    ll.append(chr(97+ii))

for ii in range(10000):
    print ll

当我这样打印时,会报告一些错误,为什么?!

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r'Traceback (most recent call last):
  File "e:/czh/python/test.py", line 7, in <module>
    print ll
IOError: [Errno 0] Error

2 个答案:

答案 0 :(得分:1)

这是Windows 10版本1803中修复的Windows错误。(请参见https://bugs.python.org/issue32245https://github.com/Microsoft/vscode/issues/36630#issuecomment-385759625

在使用调用WriteFile的代码路径(即os.write和传统标准I / O模式时,它会影响Python 3.6+,并且始终会影响Python 2.7和Python 3.5。)

答案 1 :(得分:0)

另一个答案:

我在 Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) 中也遇到了同样的问题 [MSC v.1500 64 位 (AMD64)]

当我想处理一些包含汉字的文本时,我遇到了这个问题。

核心代码是:

content = fp.read().strip().strip("\n").split("\n")  # fp is an opened file obj
line = "".join([content[x] for x in [1, 4, 7, 10, 13, 16]])
print(line)

正确的方法是:在迭代之前使用 unicode 而不是 str(utf-8)

content = fp.read().strip().strip("\n").decode("utf-8").split("\n") # fp is an opened file obj
line = "".join([content[x] for x in [1, 4, 7, 10, 13, 16]])
print(line)
相关问题