Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

file-io

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 when those bytes exist to be read?

like image 702
Jegschemesch Avatar asked Oct 21 '25 05:10

Jegschemesch


1 Answers

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.

like image 76
c0da Avatar answered Oct 25 '25 18:10

c0da