0

I've looked around, but can't figure out why my 3rd function, info_count(), is running on an infinite loop:

def error_statement():
    errorLog = open("error_log.txt", "r")
    for line in errorLog:
        if "[error]" in line:
            print(line)

def statistics_statement():
    errorLog = open("error_log.txt", "r")
    for line in errorLog:
        if "statistics" in line:
            print(line)

def info_count():
    errorLog = open("error_log.txt", "r")
    count = 0
    for line in errorLog:
        if "[info]" in line:
            count += 1
        print(count)

error_statement()
statistics_statement()
info_count()

The first two return the proper results and ends. But my count keeps looping and I don't see why it doesn't break at the end of the run.

In addition, once I get that count, I want to later print those lines out, but only a specific section ie the IP address, which may vary on each line that returns "[info]". Please advise.

6
  • Can you post part of the output? Commented May 21, 2020 at 3:57
  • 1
    In info_count(), The print(count) is within the for loop. You might want to print the count outside the loop and then post a sample output? Commented May 21, 2020 at 3:59
  • @AnnZen output: 0 0 0 0 0 1 1 2 2 2 2 2 2 ... It doesn't count past 2, but keeps going. Commented May 21, 2020 at 4:02
  • It would be better to close the file in each function. Commented May 21, 2020 at 4:03
  • Why don't you change the errorLog = open("error_log.txt", "r") to with open("error_log.txt", "r") as errorLog ? Commented May 21, 2020 at 4:07

2 Answers 2

2

Do you mean why the program prints count many time? If so, you should remove an indent:

def info_count():
    errorLog = open("error_log.txt", "r")
    count = 0
    for line in errorLog:
        if "[info]" in line:
            count += 1
    print(count)
Sign up to request clarification or add additional context in comments.

2 Comments

Wow... thank you. Indentation was the answer... know how I can make another function to get those lines and output a different part of the line? ie I'd like that IP address to show: [Mon Mar 8 11:45:08 2004] [info] [client 64.242.88.10] (104)Connection reset by peer: client stopped connection before send body completed
thank you so much. This work as intended. I'll have to read through it and see what it is doing so I can replicate it in different situations. I saw you have a YouTube channel and plan to browse some of your work later!
1

For the IP addresses (requested in the comments):

import re

def ip():
    error_log = open('error_log.txt','r')
    ip = re.findall('\[client .*?\]', error_log.read())
    print('\n'.join([x[8:-1] for x in ip]))
ip()

Square brackets are special characters, so we want to tell python that we only want them as part of the string, To do that, right before the special character, we put a backslash.

The * is to tell python we want all the characters between [client and ].

The . dot tells the program that we accept any character besides newlines.

The ? is to tell python not to be greedy.

For example, say this is our string: 'Hello, my name is Ann. What is your name?'

We want to find all the characters between 'm' and 'n'.

If our program is greedy, it will give us ['y name is Ann. What is your '], if not greedy, ['y ', 'e is A'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.