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

Jonathan Toomim jtoomim at jtoomim.org
Wed Jun 12 23:19:44 CEST 2013


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:
         matching = False
         
         # uncomment the line below if you want to only
         # print out the first matching section
         
         # break

f.close()



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



More information about the BangPypers mailing list