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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work! It only prints one line next to line starting with 'Test Failed:'
@gdi, make some edits; let me know if still doesn t wor

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.