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

Vishal vsapre80 at gmail.com
Sun Jun 16 21:52:23 CEST 2013


Hi,

If the file you are talking about is not too big...i mean has less than say
50,000 lines, then the simplest would be to do:

with open('fileName') as fin:
    lines = fin.readlines()

printStuff = False
for line in lines:
    if 'start_keyword' in line:
        printStuff = True
        print line

    elif 'stop_keyword' in line:
        print line
        break

    elif printStuff:
        print line

Hope this helps,
Vishal Sapre



Thanks and best regards,
Vishal Sapre

---
"Life is 10% how you make it, and 90% how you take it"
"बहुजन हिताय, बहुजन सुखाय (Benefit for most people, Happiness for most
people.)"
---
Please DONT print this email, unless you really need to. Save Energy &
Paper. Save the Earth.


On Thu, Jun 13, 2013 at 1:18 AM, davidsnt <davidsnt at gmail.com> 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