eof

Wayne Brehaut wbrehaut at mcsnet.ca
Wed Nov 21 19:41:16 EST 2007


Hi braver,

On Wed, 21 Nov 2007 15:17:14 -0800 (PST), braver
<deliverable at gmail.com> wrote:

>I'd like to check, for a filehandle f, that EOF has been reached on
>it.  What's the way to do it?  I don't want to try/except on EOF, I
>want to check, after I read a line, that now we're in the EOF state.

It would be nicer to check the reference manual than ask here.  If you
have PythonWin 2.5.1 (r251:54863, May  1 2007, 17:47:05) [MSC v.1310
32 bit (Intel)] on win32.for example, using Help, Index, eof gives: 

eof 
Token used to determine end of file. This will be set to the empty
string (''), in non-POSIX mode, and to None in POSIX mode. New in
version 2.3. 

If you don't use Active State Python--and even of you do--it helps to
have these  three "official" references handy:

===
http://docs.python.org/ref/ref.html

Python Reference Manual
Guido van Rossum

Python Software Foundation 
Email: docs at python.org 

Fred L. Drake, Jr., editor

Release 2.5
19th September, 2006
===
http://docs.python.org/lib/lib.html

Python Library Reference
Guido van Rossum

Python Software Foundation 
Email: docs at python.org 

Fred L. Drake, Jr., editor

Release 2.5
19th September, 2006
===
http://docs.python.org/tut/tut.html

Python Tutorial
Guido van Rossum

Python Software Foundation 
Email: docs at python.org 

Fred L. Drake, Jr., editor

Release 2.5
19th September, 2006
===

The tutorial gives simpler explanations and examples, including:

7. Input and Output 
7.2.1 Methods of File Objects 

>>> f.read()
'This is the entire file.\n'
>>> f.read()
''
===

If the end of the file has been reached, f.read() will return an empty
string (""). 

By browsing the index or TOC, or searching, or guessing, you should
conclude that you want 

3.9 File Objects

There, and checking for "EOF"  you'll note that both read( [size]) and
readline( [size]) include:

"An empty string is returned only when EOF is encountered
immediately."

HTH?

>In Ruby it's f.eof:

It also is not nice to talk Ruby here--nor Perl. Refer to C/C++ if
necessary.

wwwayne


>
>In Ruby:
>>> f = File.open("jopa")
>=> #<File:jopa>
>>> f.read()
>=> "jopa\n"
>>> f.eof
>=> true
>
>Is there a Python analog?
>
>Cheers,
>Alexy



More information about the Python-list mailing list