how to know the end of a file(use readline)

Chris Barker chrishbarker at home.net
Tue Jul 31 13:20:17 EDT 2001


Alex Martelli wrote:
> > I use readline to process the whole ,but
> > I do not know how to control it,when it
> > is the end of the file
> 
> f.readline() returns an empty string, '',
> when f is at end of line, and in no other
> case (when reading an empty line, it

So the common idiom is:

while 1:
    line = file.readline()
    if not line:
	break()
    ...do stuff with line...

You also might want to use xreadlines (or readlines):

for line in file.xreadlines():
    do stuff with line






-- 
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