Unable to store terminal output of subprocess with python
Question: My code has two potential outcomes in the terminal: `Can't connect RFCOMM
socket: Permission denied` and `Can't connect RFCOMM socket: Host is down`. I
need to store either result as a string in a variable, but everything I've
tried has failed. This is the code that I thought would do it:

    
    
    from subprocess import check_output
    
    out = check_output(["sudo", "rfcomm", "connect", "0", "AA:BB:CC:DD:EE:FF", "10"])
    print "output: %s" % out
    

Instead I get nothing:

    
    
    user:~/home $./foo.py
    Can't connect RFCOMM socket: Permission denied
    output:
    

Another attempt:

    
    
    proc = subprocess.Popen(["sudo rfcom connect 0 AA:BB:CC:DD:EE:FF 10"], stdout=subprocess.PIPE, shell=True)
    (out, err) = proc.communicate()
    print "output: %s" % out, err
    

This at least gives me something when I print. Unfortunately it's "None"
telling me there is no error and not the actual output:

    
    
    user:~/home $./foo.py
    Can't connect RFCOMM socket: Permission denied
    output:  None
    

I've already tried [this](http://stackoverflow.com/questions/2502833/store-
output-of-subprocess-popen-call-in-a-string)
[this](http://stackoverflow.com/questions/19243020/in-python-get-the-output-
of-system-command-as-a-string)
[this](http://stackoverflow.com/questions/3503879/assign-output-of-os-system-
to-a-variable-and-prevent-it-from-being-displayed-on)
[this](http://stackoverflow.com/questions/4514751/pipe-subprocess-standard-
output-to-a-variable) and probably a couple others. I'm sure I'm missing a
piece of critical knowledge somewhere. Thanks for any pointers!


Answer: `rfcomm` is apparently writing its output to standard error, yet you are only
capturing standard output. To capture both, include `stderr=subprocess.STDOUT`
in the call to `check_output`:

    
    
    subprocess.check_output(["sudo", "rfcomm", "connect", "0", "AA:BB:CC:DD:EE:FF", "10"],
                            stderr=subprocess.STDOUT)
    

