Checking for EOF in stream
Grant Edwards
grante at visi.com
Mon Feb 19 21:58:41 EST 2007
On 2007-02-20, GiBo <gibo at gentlemail.com> wrote:
>>> stream = sys.stdin
>>> while True:
>>> data = stream.read(1024)
>> if len(data) == 0:
>> break #EOF
>>> process_data(data)
>
> Right, not a big difference though. Isn't there a cleaner /
> more intuitive way?
A file is at EOF when read() returns ''. The above is the
cleanest, simplest, most direct way to do what you specified.
Everybody does it that way, and everybody recognizes what's
being done.
It's also the "standard, Pythonic" way to do it.
> Like using some wrapper objects around the streams or
> something?
You can do that, but then you're mostly just obfuscating
things.
--
Grant Edwards grante Yow! Vote for ME
at -- I'm well-tapered,
visi.com half-cocked, ill-conceived
and TAX-DEFERRED!
More information about the Python-list
mailing list