-1

In shell, cat filename | grep -i error would return content from the file having the string 'error' .

What is the Python equivalent of this ?

1 Answers1

2

Open the file, iterate over all the lines and print the lines only if it contains error.

with open(file) as f:
    for line in f:
        if 'error' in line:
            print(line)

for case-insensitive match,

with open(file) as f:
    for line in f:
        if re.search(r'(?i)error', line):
            print(line)
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274