Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)
edited by

I am trying to scan through log files and match certain strings (operators). Once I match the operator, I would liked to look for another string that will specify "success" of a task in a log file. My loop is only working before the first elif.. the variable operator_type is defined correctly. I tested it by printing operator_type. I don't fully understand using "break" so those maybe the issue. The code is jumping all the way down to the else (based on my output).

fl_sts == 'FAILED' My code is able to find failures. It is unable to find successes.

for i, line in enumerate(lines):

    if (fl_sts in line):

        end_line = lines[i]

        success_status = fl_sts

        break

    elif operator_type == op_py in line:

        for line in lines:

            if (sc_crt_py in line):

                end_line = lines[-1]

                success_status = sc_sts

            else:

                break

    elif operator_type == op_eml in line:

        for line in lines:

            if (sc_crt_eml in line):

                end_line = lines[-1]

                success_status = sc_sts

            else:

                break

    elif operator_type == op_s3 in line:

        for line in lines:

            if (sc_crt_s3 in line):

                end_line = lines[-1]

                success_status = sc_sts

            else:

                break

    elif operator_type == op_bsh in line:

        for line in lines:

            if (sc_crt_bsh in line):

                end_line = lines[-1]

                success_status = sc_sts

            else:

                break

    elif operator_type == op_snw in line:

        for line in lines:

            if (sc_crt_snw in line):

                end_line = lines[-1]

                success_status = sc_sts

            else:

                break

    elif operator_type == op_snw_sns in line:

        for line in lines:

            if (sc_crt_snw_sns in line):

                end_line = lines[-1]

                success_status = sc_sts

            else:

                break

    else:

        success_status = sc_sts_rn

        end_line = 'None'

1 Answer

0 votes
by (25.1k points)
edited by

Remove the breaks.

You are exiting the loop after the first If.

Related questions

0 votes
2 answers
0 votes
1 answer
asked Feb 10, 2021 in Python by ashely (50.2k points)
0 votes
1 answer

Browse Categories

...