Skip to main content
 首页 » 编程设计

python之为什么 Python 2.x 的 write() 函数不返回写入的字节数

2025年05月04日9lovecherry

我最感兴趣的是导致 Python 2.x 的 write() 函数返回 None 而不是写入的字节数的设计决策。这意味着 File 对象有一些不同的心智模型——它不仅仅是 UNIX 文件描述符的轻量级包装器。

这是我所指的文档:http://docs.python.org/library/stdtypes.html#file.write

请您参考如下方法:

来自Python官方手册3.2.2:-

write(b)  

Write the bytes or bytearray object, b and return the number of bytes written. When in non-blocking mode, a BlockingIOError is raised if the buffer needs to be written out but the raw stream blocks.