readlines()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • techyvibe
    New Member
    • Feb 2008
    • 5

    readlines()

    i seem to have a problem with readlines()as below

    i have

    global f

    f=open("hello.t xt", "r")

    f1 = open("C:\\log1. txt",'w')
    f2 = open("C:\\log2. txt",'w')

    def new1():
    for line in f.readlines():
    if line.find('new' )>= 0:
    f1.write("%s" %line)


    def new2():
    for line in f.readlines():
    if line.find('abc' )>= 0:
    f2.write("%s" %line)

    new1()
    new2()



    i don't seem to get an output for new2 ,i mean it doesn't go through the for loop


    Am i doing anything wrong ??
  • diegososa
    New Member
    • Oct 2007
    • 19

    #2
    At first: Post code between [code ] [ /code] tags ...

    Second: the "f" file object has like a "pointer" pointing where it's reading... after a "for" loop, that pointer keeps on EOF until you reload the file (re create the object) or until you do f.seek(0).
    That's why function 1 works and function 2 doesn't...

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Very good diegososa. Instead if this:[code=Python]if line.find('abc' )>=0:[/code]
      try this:[code=Python]if 'abc' in line:[/code]

      Comment

      • techyvibe
        New Member
        • Feb 2008
        • 5

        #4
        Thanks a lot for replying ...i appreciate it :)

        Comment

        Working...