0

I have to run the UNIX specific command using python and capture only the line after "Test Failed: " line. The approach I used is:

import os
def system_check(command: str):
    stream = os.popen(command)
    output = stream.readlines()
    for line in output:
        if line.strip().startswith('Test Failed: '):
            for line in output:
                print(line)

This reads every line starting from the beginning, not only after "Test Failed". If I use file reading as in How to only read lines in a text file after a certain string? it works.

gd1
  • 655
  • 1
  • 10
  • 21

1 Answers1

0

Well, the solution to that question is correct since the user wants all lines after a specific string.

To your question, however, you try this, without using another loop:

for line_nr, line in enumerate(output):
        if line.startswith('Test Failed: '):
            print(output[line_nr+1])

Or:

`for line_nr, line in enumerate(output):
   if 'Test Failed: ' in line:
     print(output[line_nr+1])`

On a second note, to why does it print all lines (event those that are before 'Test Failed: ') perhaps it might had to do it with the strip() method and the content of the output.

andreis11
  • 1,133
  • 1
  • 6
  • 10