readline skips on line

Amit Patel amitp at Xenon.Stanford.EDU
Thu Apr 13 21:59:35 EDT 2000


 Soundwave <maybe at lucky.com> wrote:
| I got a part of a programm looking like this
| 
| f=open('test.txt', 'r')
| while f.readline()!='':
|     print f.readline()
| print 'ready'
| 

You're calling readline() twice.  The first line is checked to see if
it is != '', and the second line is printed.  The third line is
checked to see if it is != '', and the fourth line is printed.  And so
on.  Try this:

f = open('test.txt', 'r')
while 1:
    line = f.readline()
    if line == '': break
    print line
print 'ready'


      - Amit

--
Amit J Patel, Computer Science Department, Stanford University
http://www-cs-students.stanford.edu/~amitp/





More information about the Python-list mailing list