If you have Python version 2.7 or later, you can use subprocess.check_output which basically does exactly what you want (it returns standard output as a string).
A simple example (Linux version; see the note):
import subprocess
print subprocess.check_output(["ping", "-c", "1", "8.8.8.8"])
Note that the ping command is using the Linux notation (-c for count). If you try this on Windows, remember to change it to -n for the same result.
As commented below, you can find a more detailed explanation in this other answer.
Answer from sargue on Stack OverflowIf you have Python version 2.7 or later, you can use subprocess.check_output which basically does exactly what you want (it returns standard output as a string).
A simple example (Linux version; see the note):
import subprocess
print subprocess.check_output(["ping", "-c", "1", "8.8.8.8"])
Note that the ping command is using the Linux notation (-c for count). If you try this on Windows, remember to change it to -n for the same result.
As commented below, you can find a more detailed explanation in this other answer.
Output from subprocess.call() should only be redirected to files.
You should use subprocess.Popen() instead. Then you can pass subprocess.PIPE for the stderr, stdout, and/or stdin parameters and read from the pipes by using the communicate() method:
from subprocess import Popen, PIPE
p = Popen(['program', 'arg1'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
rc = p.returncode
The reasoning is that the file-like object used by subprocess.call() must have a real file descriptor, and thus implement the fileno() method. Just using any file-like object won't do the trick.
See here for more info.
Capturing output from subprocess.run()
You’ll need to use the stdout and/or stderr arguments. https://docs.python.org/3/library/subprocess.html#subprocess.run
Edit: link to #subprocess.run
More on reddit.compython - Store output of subprocess.Popen call in a string - Stack Overflow
shell - python getoutput() equivalent in subprocess - Stack Overflow
How can I retrieve the output of a process run using Python subprocess.call? - Ask a Question - TestMu AI Community
Videos
Lets say I run:
subprocess.run(["wmctrl", "-l"])
This is great as it lists all open windows! However I can't figure out how to put this output into a variable of some kind so I can do something with it. Any help is appreciated.
You’ll need to use the stdout and/or stderr arguments. https://docs.python.org/3/library/subprocess.html#subprocess.run
Edit: link to #subprocess.run
Thanks for the answers. In the mean time I did get this to work...
import subprocess
output = subprocess.check_output("wmctrl -l", shell=True)
# it returns a byte type so convert it to string
output = str(output)
# split it into a list that makes sense
output = output.split('\\n')
# print out our list of windows
for x in range(len(output)):
print (output[x])
Thanks again! Just thought I'd share what worked for me for other beginners.
In Python 2.7 or Python 3
Instead of making a Popen object directly, you can use the subprocess.check_output() function to store output of a command in a string:
from subprocess import check_output
out = check_output(["ntpq", "-p"])
In Python 2.4-2.6
Use the communicate method.
import subprocess
p = subprocess.Popen(["ntpq", "-p"], stdout=subprocess.PIPE)
out, err = p.communicate()
out is what you want.
Important note about the other answers
Note how I passed in the command. The "ntpq -p" example brings up another matter. Since Popen does not invoke the shell, you would use a list of the command and options—["ntpq", "-p"].
In Python 3.7+ you can use the new capture_output= keyword argument for subprocess.run:
import subprocess
p = subprocess.run(["echo", "hello world!"], capture_output=True, text=True)
assert p.stdout == 'hello world!\n'
Use subprocess.Popen:
import subprocess
process = subprocess.Popen(['ls', '-a'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
print(out)
Note that communicate blocks until the process terminates. You could use process.stdout.readline() if you need the output before it terminates. For more information see the documentation.
For Python >= 2.7, use subprocess.check_output().
http://docs.python.org/2/library/subprocess.html#subprocess.check_output