Hiding shell output from subprocess.check_output ()

I get information about my wireless connection using

In [4]: subprocess.check_output('iwconfig')
eth0      no wireless extensions.

lo        no wireless extensions.

Out[4]: 'wlan0 ...'


I get the line I want, but I would like to clear the shell of information about eth0

and lo

(note that these lines are not on the return line check_ouput

, they are printed to the shell between ipython In [4]

and Out [4]


My final guess is that these two lines are considered a warning and not caught check_output

. Hoping that they are output to stderr

, I tried several options

iwconfig > /dev/null 2>&1  # empties my wanted string
iwconfig 2 > /dev/null     # throws an error


but without success so far.

How can I prevent check_output

shell output ?


source to share

1 answer

If you want stderr

like yours stdout


subprocess.check_output('iwconfig', stderr=subprocess.STDOUT)


If you want to just destroy your stderr


import os
subprocess.check_output('iwconfig', stderr=open(os.devnull, 'w'))




All Articles