Lookahead while doing: for line in fh.readlines():

Terry Reedy tjreedy at udel.edu
Sat Feb 27 06:46:24 EST 2016


On 2/27/2016 4:39 AM, Veek. M wrote:
> I want to do something like:
>
> #!/usr/bin/env python3
>
> fh = open('/etc/motd')
> for line in fh.readlines():
>      print(fh.tell())
>
> why doesn't this work as expected.. fh.readlines() should return a
> generator object and fh.tell() ought to start at 0 first.

Not after you have already read some data.  Readlines() reads the entire 
file and splits it into lines.  readline reads at least a single block. 
  Reading a single byte or character at a time looking for /n would be 
too slow, so even after readline, the file pointer will be somewhere 
past the end of the last line returned.

> Instead i get the final count repeated for the number of lines.
>
> What i'm trying to do is lookahead:
> #!whatever
>
> fh = open(whatever)
> for line in fh.readlines():
>      x = fh.tell()
>      temp = fh.readline()
>      fh.seek(x)
>


-- 
Terry Jan Reedy




More information about the Python-list mailing list