1

here is my problem: I need to execute a cmd command from python, the command is not something that executes fast, it will be running for a longer period of time. What I need is a way to #1 execute the command and #2 get the continuous output of that command.

Example of the output:
some text... (then about half a minute wait)
some other text... (waiting for some time)
more text...

You get the idea, the output comes about every 30 seconds. I need to catch all of the outputs and do something with them. Is there a way to do this?

I know I can execute the command with os.system('command') but i'm struggling to find a way to read the output!

1
  • 2
    Look at the subprocess module. It's a bit of a steep learning curve, but doing what you want has more variations than you expect. os.system() has been deprecated for nearly 20 years (since Python 2.4), one of the reasons being the problem you have encountered. Commented Mar 17, 2022 at 10:25

1 Answer 1

1

A simple solution is given in this answer. In python 3 it would be something like:

#!/usr/bin/env python3
import subprocess

proc = subprocess.Popen('./your_process', stdout=subprocess.PIPE)

while True:
    line = proc.stdout.readline()
    if not line:
        break
    else: 
        # Do whathever you need to do with the last line written on 
        # stdout by your_process
        print("doing some stuff with...", line)
        print("done for this line!")

of course, you'd still need to take into account how the producer process buffers stdout, i.e. if the subprocess is something like

#include <stdio.h>
#include <unistd.h>

int main()
{
    for (int i = 1; i < 10000000; i++) {
        printf("ping");
        sleep(1);
    }
}

the consumer script will just read a lot of "ping"s all together (which can be resolved with a fflush(stdout) in the loop.)

A much more complicated solution can be found in this answer.

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

Comments

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.