0

I'm new to python and not looking for the "fast way" but for a hint.

I want to check a file for the pattern "error: " and print it with the line number so that I later on know where to look. If however the value is 0 (means "error: 0") this should not trigger.

Here's my "match all error:" script:

#!/usr/bin/env python

import re
import sys

lookup = 'errors: '
lookup2 = 'errors: 0'

with open(sys.argv[1]) as myFile:
    for num, line in enumerate(myFile, 1):
        if lookup in line:
            print('Line:', num, line)

I tried the "not" statament but this doesn't work:

#!/usr/bin/env python

import re
import sys

lookup = 'errors: '
lookup2 = 'errors: 0'

with open(sys.argv[1]) as myFile:
    for num, line in enumerate(myFile, 1):
        if lookup in line not lookup2:
            print('Line:', num, line)

Any hints? Sorry for my probably very basic question...

Chris929
  • 23
  • 5

1 Answers1

1

Based on what I understood. You want to print content if string errors: is there but if errors: 0 is there then do not print.

with open(sys.argv[1]) as myFile:
    for num, line in enumerate(myFile, 1):
        if lookup in line and lookup2 not in line:
            print('Line:', num, line)
krishna
  • 1,029
  • 6
  • 12