How do I wait for a subprocess to finish, store it's output and add a prefix (without a new line) to the output?

LogicalBranch

I have a Python(3) script that's calling an external command using the subprocess.call([...]) method:

import subprocess

print("Prefix: ")
subprocess.call(["cmd", "--option", "filename.x"])

The command executes without any errors but the problem is the output.

The output isn't "uniform" and sometimes the program will output:

Program output...
Prefix:

And other times the output will be:

Prefix:
Program output....

The result I'm looking for is:

Prefix: Program output...

I know that in order to achieve this result I need to wait for the subprocess to finish, store it's output and then print the prefix (without \n) with the subprocess' output after it, I just can't figure out how to do it.

Thanks.

LogicalBranch

First you need to import the sys module, so you can use sys.stdout's write and flush methods.

import sys

You'll also need to import the subprocess module so you can use the subprocess.check_output method.

import subprocess

Use sys.stdout.write instead of print:

sys.stdout.write("Prefix: ")

Then you'll need to replace subprocess.call with subprocess.check_output, which runs the given command and waits for the output.

response = subprocess.check_output(["cmd", "--option", "filename.x"])

NOTE: you need to decode the response because it's a bytes object and not a string.

sys.stdout.write(response.decode("UTF-8"))

And finally you need to flush the output:

sys.stdout.flush()

Here is the final result:

import sys, subprocess
sys.stdout.write("Prefix: ")
response = subprocess.check_output(["cmd", "--option", "filename.x"])
sys.stdout.write(response.decode("UTF-8"))
sys.stdout.flush()

Good luck, hopefully no one else will stumble on this question like I did.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get output on same line

How do I add a new line to a richtextbox without making the last line blank?

How to search for line with subprocess.check_output?

How to store old txt lines and store new output to new line on iostream (C++)

How to add specific prefix to the logging output in Rust?

How do I output a javascript array where each array item is a new line using JSON.stringify

How do I store a functions output without re-running the function in Spyder

How do I output JSchema without the formatting?

How do I prepend a string to program output without waiting for the whole line?

How do I add a prefix/suffix to each line of a file?

How do I add a new line to richTextBox?

How do I output this line to a remote file?

How to add a different prefix to every line in a grep output?

How do I output the acronym on one line

How to add new line after bash command before output

How to add custom prefix to json output with PHP

How to add new line between `ls -l` output

How do i store output of the function into an array

How do I hide the output from a subprocess and continue the script execution?

How can I write to Netcat output in Powershell without a new line being appended to the end?

How to print output on new line?

How do i apply a command to each line in the output from pipe and add all those numbers?

How do I print the output in one line as opposed to it creating a new line?

How do I add space or comma in this output?

how do I add color to output in vscode

how do I wait for database call to finish

How to add new line in xsl with output to html

How do I read the current output with asynchronous without waiting for the process to finish?

BASH: add prefix to each line on curl output