Since the commands module is deprecated since Python 2.6, I’m looking into the best way to replace commands.getstatusoutput which returns a tuple of the command’s return code and output. The subprocess module is rather obvious, however, it doesn’t offer a direct replacement for getstatusoutput. A potential solution is discussed in a related question concerning getstatusoutput – however, I’m not looking into rewriting the original function (which has less then 10 LOC anyway) but would like to know if there is a more straightforward way.
Since the commands module is deprecated since Python 2.6, I’m looking into the best
Share
There is no direct replacement, because
commands.getstatusoutputwas a bad API; it combines stderr and stdout without giving an option to retrieve them separately.The convenience API that you should be using is
subprocess.check_outputas it will throw an exception if the command fails.Otherwise, it does appear somewhat of a deficiency that
subprocessdoesn’t provide a method to retrieve output and status in a single call, but it’s easy to work around; here’s what the answer to the linked question should have been:If you want
stdoutandstderrtogether, usestderr=subprocess.STDOUT.