from subprocess import check_output, STDOUT cmd = "some cmd"
try: cmd_stdout = check_output(cmd,stderr=STDOUT, shell=True).decode() except Exception as e: print(e.output.decode()) # print out the stdout messages up to the exception print("a") # To print out the exception message