[Python-Dev] fileobj.read(float): warning or error?

Guido van Rossum guido at python.org
Tue Sep 2 19:11:29 CEST 2008


On Tue, Sep 2, 2008 at 10:05 AM, Jesus Cea <jcea at jcea.es> wrote:
> Guido van Rossum wrote:
>> On Mon, Jul 21, 2008 at 10:37 PM, Cameron Simpson <cs at zip.com.au> wrote:
>>> Leaving aside the 0.2 => 0 converstion, shouldn't read() raise an
>>> exception if asked for < 1 bytes? Or is there a legitimate use for read(0)
>>> with which I was not previously aware?
>>
>> Indeed. read(0) is quite often generated as an edge case when one is
>> computing buffer sizes, and returning an empty string is most
>> definitely the right thing to do here (otherwise some application code
>> becomes more complex by having to avoid calling read(0) at all).
>
> How do you differenciate between that empty string (when doing
> "read(0)"), from EOF (that is signaled by an empty string)?.

You don't. If you want to know whether you hit EOF you should try
reading a non-zero number of bytes. (Also note that getting fewer
bytes than you asked for is not enough to conclude that you have hit
EOF.)

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list