Loop function works first time, not second time

Posted by user1483101 on Stack Overflow See other posts from Stack Overflow or by user1483101
Published on 2012-06-26T14:49:08Z Indexed on 2012/06/26 15:16 UTC
Read the original article Hit count: 133

Filed under:
|

I'm creating a parsing program to look for certain strings in a a text file and count them. However, I'm having some trouble with one spot.

def callbrowse():
    filename = tkFileDialog.askopenfilename(filetypes = (("Text files", "*.txt"),("HTML files", ".html;*.htm"),("All files", "*.*")))
    print filename
    try:
        global filex
        global writefile
        filex = open(filename, 'r')
        print "Success!!"
        print filename
    except:
        print "Failed to open file"

######This returns the correct count only the first time it is run.  The next time it  ######returns 0.  If the browse button is clicked again, then this function returns the ######correct count again.
def count_errors(error_name):
    count = 0
    for line in filex:
        if error_name == "CPU > 79%":
            stringparse = "Utilization is above"
        elif error_name == "Stuck touchscreen":
            stringparse = "Stuck touchscreen"
        if re.match("(.*)" + "Utilization is above" + "(.*)",line):
            count = count + 1
    return count

Thanks for any help. I can't seem to get this to work right.

© Stack Overflow or respective owner

Related posts about parsing

Related posts about loops