How to suppress or capture the output of subprocess.run()?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Digital Sunset Looping
--
Chapters
00:00 How To Suppress Or Capture The Output Of Subprocess.Run()?
00:24 Accepted Answer Score 400
02:13 Answer 2 Score 30
02:23 Thank you
--
Full question
https://stackoverflow.com/questions/4117...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #subprocess
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Digital Sunset Looping
--
Chapters
00:00 How To Suppress Or Capture The Output Of Subprocess.Run()?
00:24 Accepted Answer Score 400
02:13 Answer 2 Score 30
02:23 Thank you
--
Full question
https://stackoverflow.com/questions/4117...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #subprocess
#avk47
ACCEPTED ANSWER
Score 400
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.DEVNULLtarget. 
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/nullby 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
 strinstead ofbytes.
- Omit
 universal_newlines=Trueto getbytesdata- Python >= 3.7 accepts
 text=Trueas a short form foruniversal_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.PIPEto capture STDOUT and STDERR independently. This works on any version of Python that supportssubprocess.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)
ANSWER 2
Score 30
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)