除了EOF之外,Python的read()总是返回请求的大小吗?

时间:2011-09-12 04:59:02

标签: python file-io

Python read()方法是否像C的读取一样?在到达文件的最后一个块之前,它可能返回少于请求的字节数吗?或者它是否保证在读取这些字节时始终返回完整的字节数?

2 个答案:

答案 0 :(得分:2)

嗯,Python标准库说这是关于file.read([size]):

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes).如果size参数为负数或省略,read all data until EOF is reached。 ...... An empty string is returned when EOF is encountered immediately。 ...另请注意when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.

答案 1 :(得分:1)

在CPython上,它将始终返回请求的字节数,除非达到EOF。