Reputation: 16065
From the examples in docs on subprocess.run()
it seems like there shouldn't be any output from
subprocess.run(["ls", "-l"]) # doesn't capture output
However, when I try it in a python shell the listing gets printed. I wonder if this is the default behaviour and how to suppress the output of run()
.
Upvotes: 246
Views: 323744
Reputation: 48735
Here is how to suppress output, in order of decreasing levels of cleanliness. They assume you are on Python 3.
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
)
/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
)
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 ofbytes
.
- Omit
universal_newlines=True
to getbytes
data- Python >= 3.7 accepts
text=True
as a short form foruniversal_newlines=True
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)
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)
Upvotes: 414
Reputation: 417
ex: to capture the output of ls -a
import subprocess
ls = subprocess.run(['ls', '-a'], capture_output=True, text=True).stdout.strip("\n")
print(ls)
Upvotes: 30