Reputation: 54013
I'm running some terminal commands from within Python using the subprocess.check_output() call. This works all fine when it returns correct results. When interacting with the bitcoin daemon I can get several responses however. For example, the raw output (as seen on a normal bash command line) can be:
: Cannot obtain a lock on data directory /home/kramer65/.bitcoin. Bitcoin is probably already running.
or
error: couldn't connect to server
Both these responses give an error code 1 however. I tried printing out the following attributes of the subprocess.CalledProcessError as e
, which in both cases results in the same outputs (except for the cmd
attribute of course:
print e.args, e.cmd, e.message, e.output, e.returncode
# () 'BTC' 1
# () 'BTC getinfo' 1
I guess the only thing that distincts the two errors, is the raw string that is outputted on the command line which I listed above. So my question is: how can I get the raw string that is shown on the command line from within Python?
Upvotes: 3
Views: 453
Reputation: 363566
It is likely coming on the stderr
.
You could setup your subprocess with
stderr=subprocess.STDOUT
as a kwarg to merge the stdout and stderr together.
Alternatively, if you need them separately, do something like
proc = subprocess.Popen(..., stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
Note: unlike check_output
, this method will not raise exception if the return code was nonzero, so you will have to do that manually if that's the behaviour you wanted.
Upvotes: 3