Search code examples
pythonpython-2.7subprocessstderr

Get only stderr output from subprocess.check_output


I want to run an external process in python, and process its stderr only.

I know I can use subprocess.check_output, but how can I redirect the stdout to /dev/null (or ignore it in any other way), and receive only the stderr?


Solution

  • I found a simple trick:

    import subprocess
    stderr_str = subprocess.check_output('command 2>&1 >/dev/null')
    

    This will filter out the stdout, and keeps only the stderr.