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 Overflow
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
3 days ago - Catching this exception and retrying communication will not lose any output. Supplying input to a subsequent post-timeout communicate() call is in undefined behavior and may become an error in the future. The child process is not killed if the timeout expires, so in order to cleanup properly a well-behaved application should kill the child process and finish communication: proc = subprocess.Popen(...) try: outs, errs = proc.communicate(timeout=15) except TimeoutExpired: proc.kill() outs, errs = proc.communicate()
Discussions

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.com
🌐 r/learnpython
5
7
January 29, 2018
python - Store output of subprocess.Popen call in a string - Stack Overflow
I'm trying to make a system call in Python and store the output to a string that I can manipulate in the Python program. #!/usr/bin/python import subprocess p2 = subprocess.Popen("ntpq -p") I've tried a few things including some of the suggestions here: ... It is good always to post actual code you ran and the actual traceback or unexpected bahaviour for concrete questions like this. For example, I do not know what you tried to do to get ... More on stackoverflow.com
🌐 stackoverflow.com
shell - python getoutput() equivalent in subprocess - Stack Overflow
To catch errors with subprocess.check_output(), you can use CalledProcessError. More on stackoverflow.com
🌐 stackoverflow.com
How can I retrieve the output of a process run using Python subprocess.call? - Ask a Question - TestMu AI Community
How can I retrieve the output of a process run using Python subprocess.call? I want to capture the output of a process executed using subprocess.call(). When I attempt to pass a StringIO.StringIO object to the stdout parameter, I encounter the following error: Traceback (most recent call last): ... More on community.testmuai.com
🌐 community.testmuai.com
0
December 3, 2024
🌐
GitHub
gist.github.com › nawatts › e2cdca610463200c12eac2a14efc0bfb
capture-and-print-subprocess-output.py · GitHub
Passing stdout=subprocess.PIPE, stderr=subprocess.STDOUT to subprocess.run captures the output but does not let the subprocess print. So you don't see any output until the subprocess has completed. Redirecting sys.stdout or sys.stderr doesn't work because it only replaces the Python script's stdout or stderr, it doesn't have an effect on the subprocess'.
🌐
GeeksforGeeks
geeksforgeeks.org › python › retrieving-the-output-of-subprocesscall-in-python
Retrieving the output of subprocess.call() in Python - GeeksforGeeks
July 23, 2025 - The subprocess.check_output() function runs a command and returns its output as a byte string. You can decode this byte string to get a string.
🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - Get your team access to the full DataCamp for business platform. You can use the Python subprocess module to create new processes, connect to their input and output, and retrieve their return codes and/or output of the process. In this article, we'll explore the basics of the subprocess module, including how to run external commands, redirect input and output, and handle errors.
Find elsewhere
🌐
Computer Science Atlas
csatlas.com › python-subprocess-run-stdout-stderr
Python 3: Get Standard Output and Standard Error from subprocess.run() — Computer Science Atlas
July 14, 2021 - from subprocess import run p = run( [ 'echo', 'hello' ], capture_output=True ) print( 'exit status:', p.returncode ) print( 'stdout:', p.stdout.decode() ) print( 'stderr:', p.stderr.decode() ) p.stdout and p.stderr are bytes (binary data), so if we want to use them as UTF-8 strings, we have to first .decode() them. Alternatively, on any Python 3 version that supports subprocess.run() (i.e., 3.5 and up), we can pass in subprocess.PIPE into the stdout and stderr options to capture output:
🌐
End Point Dev
endpointdev.com › blog › 2015 › 01 › getting-realtime-output-using-python
Getting realtime output using Python Subprocess | End Point Dev
January 28, 2015 - ... To run a process and read all of its output, set the stdout value to PIPE and call communicate(). import subprocess process = subprocess.Popen(['echo', '"Hello stdout"'], stdout=subprocess.PIPE) stdout = process.communicate()[0] print 'STDOUT:{}'.format(stdout)
🌐
Fabian Lee
fabianlee.org › 2019 › 09 › 15 › python-getting-live-output-from-subprocess-using-poll
Python: Getting live output from subprocess using poll | Fabian Lee : Software Engineer
September 15, 2019 - If you start a process using process.call() or process.check_output(), then you cannot get the output until the process is compete. However if you use subprocess.Popen along with Popen.poll() to check for new output, then you see a live view of the stdout.
🌐
Earthly
earthly.dev › blog › python-subprocess
How to Use Python's Subprocess Module - Earthly Blog
July 11, 2023 - The run() function returns a CompletedProcess object. You can set capture_output = True to capture the output as a string of bytes in the stdout attribute, which you can decode by calling the decode() method.
🌐
SaltyCrane
saltycrane.com › blog › 2008 › 09 › how-get-stdout-and-stderr-using-python-subprocess-module
How to get stdout and stderr using Python's subprocess module - SaltyCrane Blog
Here is how to get stdout and stderr from a program using the subprocess module: from subprocess import Popen, PIPE, STDOUT cmd = 'ls /etc/fstab /etc/non-existent-file' p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) output = p.stdout.read() print output
🌐
Learn by Example
learnbyexample.github.io › tips › python-tip-11
Python tip 11: capture external command output
June 1, 2022 - >>> import subprocess >>> cmd = ('date', '-u', '+%A') >>> p = subprocess.run(cmd, capture_output=True, text=True) >>> p CompletedProcess(args=('date', '-u', '+%A'), returncode=0, stdout='Wednesday\n', stderr='') >>> p.stdout 'Wednesday\n' >>> subprocess.check_output(cmd, text=True) 'Wednesday\n' With check_output(), you'll get an exception if something goes wrong with the command being executed.
🌐
GitHub
gist.github.com › almoore › c6fd2d041ad4f4bf2719a89c9b454f7e
Getting realtime output using Python Subprocess · GitHub
To run a process and read all of its output, set the stdout value to PIPE and call communicate(). import subprocess process = subprocess.Popen(['echo', '"Hello stdout"'], stdout=subprocess.PIPE) stdout = process.communicate()[0] print('STDO...
🌐
Medium
medium.com › @asvinjangid.kumar › how-to-store-the-output-of-a-command-in-a-variable-using-python-e7c1b8b91036
Store the Output of a Command in a Variable using Python. | by Ayush Sharma | Medium
January 7, 2024 - In this example, we use the `subprocess.check_output()` function to run the command `ls -l` and store its output in the `output` variable. By decoding the byte string, we can obtain the text representation of the output.
🌐
Lucadrf
lucadrf.dev › blog › python-subprocess-buffers
Luca Da Rin Fioretto | Capture Python subprocess output in real-time
November 20, 2022 - Unfortunately Python’s official documentation doesn’t offer any alternative solution, “There should be one – and preferably only one – obvious way to do it.” the Zen of Python says, although using Popen.communicate() to read the stdout of a piped subprocess is all but obvious.
🌐
Simplilearn
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
December 15, 2025 - Learn about Python's subprocess module for executing external commands. Discover how to manage processes and handle inputs/outputs efficiently.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
TestMu AI Community
community.testmuai.com › ask a question
How can I retrieve the output of a process run using Python subprocess.call? - Ask a Question - TestMu AI Community
December 3, 2024 - How can I retrieve the output of a process run using Python subprocess.call? I want to capture the output of a process executed using subprocess.call(). When I attempt to pass a StringIO.StringIO object to the stdout parameter, I encounter the following error: Traceback (most recent call last): File " ", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call return Popen(*popenargs, **kwargs).wait() File "/Lib...