while loop with f.readline()

William Voll zdweeb at hotmail.com
Tue Feb 13 20:32:53 EST 2001


in article mailman.982085180.24537.python-list at python.org, Chris Richard
Adams at chrisa at ASPATECH.COM.BR wrote on 2/13/01 1:29 PM:

> 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!
> 
This should get the job done.

f=open('/tmp/xferlogt','r').readlines()
for l in f:
    print l

--zdweeb




More information about the Python-list mailing list