while loop with f.readline()

Chris Richard Adams chrisa at ASPATECH.COM.BR
Tue Feb 13 13:29:00 EST 2001


I'm trying to create a loop that reads through a simple file with
strings on each line.

f=open('/tmp/xferlog', 'r')
while f.readline():
 line = f.readline()
 print line

Problem 1: Although I know there are five lines in the file - it only
prints the 3rd and 4th line.  Is my usage above correct for this - I'd
expect to see all lines.

Problem 2: How do define the range of the while loop: for example if I
place f.close() in the list of items wto go through in the loop:


f=open('/tmp/xferlog', 'r')
while f.readline():
 line = f.readline()
 print line
 f.close()

How do i code the loop to know that it should be executed after the
loop...not after each iteration???

Thanks!




More information about the Python-list mailing list