[BangPypers] [Novice] Question on File seek and tell methods

Asokan Pichai pasokan at gmail.com
Thu Jun 13 08:59:06 CEST 2013

On 13 June 2013 02:49, Jonathan Toomim <jtoomim at jtoomim.org> wrote:

> Using seek() within a for loop that reads from the same file can result in
> unintended effects. Depending on when you seek and what you seek to, you
> could end up reading the same section twice, or skipping sections, or
> getting into an infinite loop. f.next() will read a line starting from the
> current file position (though with some behind-the-scenes buffering that
> can cause issues if you do other operations on the file during the loop).
> line = f.next() is called behind the scenes at the beginning of each
> iteration of the loop.
> f.seek(f.tell(), 0) simply seeks to the current position. It does nothing
> except for incidentally flushing I/O buffers. It's also one of the weirder
> ways to do nothing; f.seek(0, 1) would make more sense ("seek 0 bytes from
> the current position").
> Note also that by the time f.seek(f.tell(), 0) is executed in your code,
> you're already past 'Chennai'. In the first instance in your example, the
> cursor position would be between the '\n' after Chennai and the 'M' of
> Mumbai.
> Regex is probably overkill for your job. A neater, more succinct idiom
> would be to use if 'Chennai' in line: if you want to do a substring match,
> or if line == 'Chennai\n': if you want to match the whole line. (if
> 'Chennai' in line: is equivalent to your current code's behavior.) If you
> want to do a case insensitive search, I suggest if 'chennai' in
> line.lower():.
> The way I would do this is as follows:
> f = open('test.log') # mode='r' is the default; no need to specify it
> except for style reasons
> matching = False
> for line in f:
>     if 'Chennai' in line:
>         matching = True
>     if matching:
>         print line
>     if 'Angry Birds' in line:
A minor defensive programming *may be* appropriate here
if 'Angry Birds' in line and matching:

>         matching = False
>                 # uncomment the line below if you want to only
>         # print out the first matching section
>                 # break
> f.close()
> It would be a good practice in general to name the start and end strings.
Of course Jonathan was focusing on the specific for loop issue and not the
general programming issues and hence hard coded the strings.

> - Jonathan
> On 6/13/2013 1:18 AM, davidsnt wrote:
>> Group,
>> I need a little help in using the file seek and tell methods, I have a
>> file
>> with n lines, in which I have to find out the line which starts from a
>> particular keyword and print all the lines until I find next matching
>> keyword.
>> I did a work around using seek and tell, I used regex as well. I opened
>> the
>> file, read line by line in a for loop, and checked each line for the
>> keyword used tell() to get the position and the seek() to move my cursor
>> and tried to print next lines until I get the next matching keyword but I
>> failed can you help me here.
>> Example test file
>> test.log
>> Bangalore
>> Hyderabad
>> Chennai
>> Mumbai
>> Tennis
>> Cricket
>> Poker
>> Angry Birds
>> Cricket
>> Mumbai
>> Chennai
>> I want to read and print from Chennai to Angry Birds
>> import sys,re
>> f = open('test.log', 'r')
>> for line in f:
>>           match = re.search('^Chennai', line)
>>           if(match):
>>                f.seek(f.tell(), 0)
>>                <I am missing out the logic from here>
>> Thanks,
>> David
>> ______________________________**_________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/**mailman/listinfo/bangpypers<http://mail.python.org/mailman/listinfo/bangpypers>
> ______________________________**_________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/**mailman/listinfo/bangpypers<http://mail.python.org/mailman/listinfo/bangpypers>

Asokan Pichai
We will find a way. Or, make one. (Hannibal)

*To find everything profound — that is an inconvenient trait.* It makes one
strain one's eyes all the time, and in the end one finds more than one
might have wished. -- Nietzsche

More information about the BangPypers mailing list