eof?

Chris Barker chrishbarker at home.net
Tue Aug 14 19:08:24 EDT 2001


Alex Martelli wrote:

> > fh = open("file", "r")
> > while (not fh.eof):
> >    fh.readline()
> 
> There are several ways.  Best overall, today (Python 2.1):
> 
>     for line in fh.xreadlines():
>         process(line)

or, if processing the file as a list of lines doesn't work, the standard
idiom is:

fh = open("file", "r")
while 1:
    line = fh.readline()
    if not line: break
    # do stuff with line


I'm not really very fond of "while 1" constructs, but since the break is
right there next to it, it's not too bad, and I think a little better
than what I used to do:

fh = open("file", "r")
line = fh.readline()
while 1:
    # do stuff with line
    # there could be a LOT of code here

    line = fh.readline()



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list