Beginner question : skips every second line in file when using readline()

Roy Smith roy at panix.com
Mon Oct 20 08:31:43 EDT 2003


"peter leonard" <pfleonard at hotmail.com> wrote:
> datafile ="C:\\Classifier\Data\\test.txt"
> dataobject = open(datafile,"r")
> 
> while dataobject.readline() !="":
> 
>         line = dataobject.readline()
>         print line

The basic problem is that you're calling readline() twice each time 
around the loop.  Once in the test part of the while (where you test and 
then throw away the returned value), and again in the body.  Of course 
you're only getting every other line!  You want to do something like 
this:

while 1:
   line = dataobject.readline()
   if line == "":
      break
   print line




More information about the Python-list mailing list