Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104032

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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From "Veek. M" <vek.m1234@gmail.com>
Newsgroups comp.lang.python
Subject Re: Lookahead while doing: for line in fh.readlines():
Date Fri, 04 Mar 2016 18:34:04 +0530
Organization Home
Lines 46
Message-ID <nbc0uf$b3m$1@dont-email.me> (permalink)
References <narqnp$4bm$1@dont-email.me> <mailman.170.1456573604.20994.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
Injection-Date Fri, 4 Mar 2016 13:01:03 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="58ec02890471a2e629698c18855939ab"; logging-data="11382"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194WQSBBO6TMdQlTdu/xi/a"
User-Agent KNode/4.14.1
Cancel-Lock sha1:5oslcZuHNN7PCR3S7EulLB8tB30=
Xref csiph.com comp.lang.python:104032

Show key headers only | View raw


Terry Reedy wrote:

> 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)
>>
> 
> 

I get that readlines() would slurp the whole file for efficiency 
reasons. Why doesn't fh.seek() work though. Object 'fh' is a data 
structure for the OS file descriptor similar to FILE in C.
<class '_io.TextIOWrapper'>

So if seek works in C, how come it doesn't work in python wrt 
readlines() which is just a method. What obviates the functioning of 
seek wrt readlines()?

fh.tell() works at the line level.. and fh.readline() works with 
fh.seek(0)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Lookahead while doing: for line in fh.readlines(): "Veek. M" <vek.m1234@gmail.com> - 2016-02-27 15:09 +0530
  Re: Lookahead while doing: for line in fh.readlines(): Terry Reedy <tjreedy@udel.edu> - 2016-02-27 06:46 -0500
    Re: Lookahead while doing: for line in fh.readlines(): "Veek. M" <vek.m1234@gmail.com> - 2016-03-04 18:34 +0530
      Re: Lookahead while doing: for line in fh.readlines(): MRAB <python@mrabarnett.plus.com> - 2016-03-04 18:36 +0000
        Re: Lookahead while doing: for line in fh.readlines(): "Veek. M" <vek.m1234@gmail.com> - 2016-03-05 09:01 +0530

csiph-web