Suppressing
Here is how to suppress output, in order of decreasing levels of cleanliness. They assume you are on Python 3.
- You can redirect to the special
subprocess.DEVNULL
target.
import subprocess
# To redirect stdout (only):
subprocess.run(
['ls', '-l'],
stdout = subprocess.DEVNULL
)
# to redirect stderr to /dev/null as well:
subprocess.run(
['ls', '-l'],
stdout = subprocess.DEVNULL,
stderr = subprocess.DEVNULL
)
# Alternatively, you can merge stderr and stdout streams and redirect
# the one stream to /dev/null
subprocess.run(
['ls', '-l'],
stdout = subprocess.DEVNULL,
stderr = subprocess.STDOUT
)
- If you want a fully manual method, can redirect to
/dev/null
by opening the file handle yourself. Everything else would be identical to method #1.
import os
import subprocess
with open(os.devnull, 'w') as devnull:
subprocess.run(
['ls', '-l'],
stdout = devnull
)
Capturing
Here is how to capture output (to use later or parse), in order of decreasing levels of cleanliness. They assume you are on Python 3.
NOTE: The below examples use universal_newlines=True
(Python <= 3.6).
- This causes the STDOUT and STDERR to be captured as
str
instead of bytes
.
- Omit
universal_newlines=True
to get bytes
data
- Python >= 3.7 accepts
text=True
as a short form for universal_newlines=True
- If you simply want to capture both STDOUT and STDERR independently, AND you are on Python >= 3.7, use
capture_output=True
.
import subprocess
result = subprocess.run(
['ls', '-l'],
capture_output = True, # Python >= 3.7 only
text = True # Python >= 3.7 only
)
print(result.stdout)
print(result.stderr)
- You can use
subprocess.PIPE
to capture STDOUT and STDERR independently. This works on any version of Python that supports subprocess.run
.
import subprocess
result = subprocess.run(
['ls', '-l'],
stdout = subprocess.PIPE,
universal_newlines = True # Python >= 3.7 also accepts "text=True"
)
print(result.stdout)
# To also capture stderr...
result = subprocess.run(
['ls', '-l'],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
universal_newlines = True # Python >= 3.7 also accepts "text=True"
)
print(result.stdout)
print(result.stderr)
# To mix stdout and stderr into a single string
result = subprocess.run(
['ls', '-l'],
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
universal_newlines = True # Python >= 3.7 also accepts "text=True"
)
print(result.stdout)
subprocess.run()
doesn't capture stdout or stderr by default, to do so requires passingPIPE
s for thestdout
and/orstderr
arguments (it's right in the linked documentation). So, unless you do, they will display as they normally would from the other process. – Arelprocess.call
), and Python 3.5 (process.run
). I came here explicitly looking for the Python 3 answer to this question. The accepted answer for this question, and the top comment on it, are both more useful in that context than the answers on the other question. – Davisoncapture_output=True
parameter. – Martsubprocess.run
is the only high-level API recommended in the subprocess module since Python 3.5. – Cheboksary