I'm using the subprocess
module from python and whenever I call any of the methods that subprocess has to run, in this case some bash code on the shell, the output (lets say from a command e.g iwconfig) is redirected to stderr.
Shouldn't the output from that command be redirected to stdout instead of to stderr? As I said, this happens on any method like .Popen()
and .check_output()
.
This question might seem duplicated, but I don't find any other answers on the forum that explains the concept of why this happens.
The stderr
parameter is set to subprocess.STDOUT
so I can grab the output from the command. Otherwise there is no other way. In any case, stderr is where the output errors go, right? This does not make sense for me...
s = subprocess.check_output("iwconfig", shell=True, universal_newlines=True, stderr=subprocess.STDOUT)
Thanks in advance.
As @JohnAnderson said, this happens because, if there is no wireless device on my system configured, the output from iwconfig
will be viewed as an error message even though the output seemed to be normal.
I also found another new method from subprocess called run() and the same happens, so definitely it is a Unix/Linux behavior.
s = subprocess.run('iwconfig', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
s.stderr #Produces output