开发者

Does Python's read() always return requested size except at EOF?

Does the Python read() method behave like C's read? Might it return less than the requested number of bytes before the last chunk of the file is reached? Or does it guarantee to always return the full amount of bytes开发者_如何学Go when those bytes exist to be read?


Well, the Python Standard library says this about file.read([size]):

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. ... An empty string is returned when EOF is encountered immediately. ... Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.


On CPython, it will always return the number of bytes requested, unless EOF is reached.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜