Issue
I am running spyder on windows 10 and when I attempt to run a command similar to the following:
cmd = 'python /path/to/program.py arg1 arg2'
subprocess.run(cmd,shell=True)
The script is being run as expected but I would like to see what is being printed to screen by the executed command in the spyder ipython console. I know the program is printing things to screen as expected by other methods (running the program from a shell) so there is not an error in the script I am running.
How do I go about enabling printing for the subprocess?
Solution
The output comes in a stream called stdout
. To capture it, you need to redirect it to a pipe, which then is terminated in the calling process. subprocess.run(...)
has builtin support for handling this:
import subprocess
cmd = 'python /path/to/program.py arg1 arg2'.split()
proc = subprocess.run(cmd, stdout=subprocess.PIPE, universal_newlines=True)
print(proc.stdout)
As can be seen, the output is caught in the CompletedProcess
object (proc) and then accessed as member data.
Also, to make the output into text (a string) rather than a bytearray, I have passed the parameter universal_newlines=True
.
A caveat, though, is that subprocess.run(...)
runs to completion before it returns control. Therefore, this does not allow for capturing the output "live" but rather after the whole process has finsihed. If you want live capture, you must instead use subprocess.Popen(...)
and then use .communicate()
or some other means of communication to catch the output from the subprocess.
Another comment I like to make, is that using shell=True
is not recommended. Specifically not when handling unknown or not trusted input. It leaves the interpretation of cmd
to the shell which can lead to all kind of security breaches and bad behavior. Instead, split cmd
into a list (e.g. as I have done) and then pass that list to subprocess.run(...)
and leave out shell=True
.
Answered By - JohanL
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.