当我在Python 2.x中使用msvcrt时,为什么会出现IOError?

时间:2016-11-16 15:34:57

标签: python windows python-2.7

我正在尝试使用Python 2.x中的HTML-PDF Web服务在Windows中生成PDF。此链接Python 2.x - Write binary output to stdout?表示如果我将其写入stdout,则需要修改二进制文件。

def generate_pdf():
    pdf = callservice(html)
    if pdf is not None and sys.platform == "win32":
        import os, msvcrt
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    return pdf

def process():
    pdf = generate_pdf()
    # This comes as IOError Errno 12 not enough space

E:\此程序运行的驱动器有10 GB可用。有谁知道会发生什么? C:\云端硬盘还有10 GB可用空间。我们应该检查msvcrt的源代码,看看发生了什么。我想检查一下。

1 个答案:

答案 0 :(得分:1)

这个answer解释了原则上发生的事情,并且追溯将揭示确切的失败呼叫。

特别是,sys.stdin.read()大于32767字节的数据块的尝试将导致IOError" [Errno 12]没有足够的空间",当没有足够的数据时阅读。考虑在Windows 7上运行以下示例:

python -c "import sys; data = sys.stdin.read(32768)"