Return (exitcode, output) of executing cmd in a shell. Execute the string 'cmd' in a shell with 'check_output' and return a 2-tuple (status, output). The locale encoding is used to decode the output and process newlines. A trailing newline is stripped from the output. The exit
(cmd, *, encoding=None, errors=None)
| 653 | # |
| 654 | |
| 655 | def getstatusoutput(cmd, *, encoding=None, errors=None): |
| 656 | """Return (exitcode, output) of executing cmd in a shell. |
| 657 | |
| 658 | Execute the string 'cmd' in a shell with 'check_output' and |
| 659 | return a 2-tuple (status, output). The locale encoding is used |
| 660 | to decode the output and process newlines. |
| 661 | |
| 662 | A trailing newline is stripped from the output. |
| 663 | The exit status for the command can be interpreted |
| 664 | according to the rules for the function 'wait'. Example: |
| 665 | |
| 666 | >>> import subprocess |
| 667 | >>> subprocess.getstatusoutput('ls /bin/ls') |
| 668 | (0, '/bin/ls') |
| 669 | >>> subprocess.getstatusoutput('cat /bin/junk') |
| 670 | (1, 'cat: /bin/junk: No such file or directory') |
| 671 | >>> subprocess.getstatusoutput('/bin/junk') |
| 672 | (127, 'sh: /bin/junk: not found') |
| 673 | >>> subprocess.getstatusoutput('/bin/kill $$') |
| 674 | (-15, '') |
| 675 | """ |
| 676 | try: |
| 677 | data = check_output(cmd, shell=True, text=True, stderr=STDOUT, |
| 678 | encoding=encoding, errors=errors) |
| 679 | exitcode = 0 |
| 680 | except CalledProcessError as ex: |
| 681 | data = ex.output |
| 682 | exitcode = ex.returncode |
| 683 | if data[-1:] == '\n': |
| 684 | data = data[:-1] |
| 685 | return exitcode, data |
| 686 | |
| 687 | def getoutput(cmd, *, encoding=None, errors=None): |
| 688 | """Return output (stdout or stderr) of executing cmd in a shell. |
no test coverage detected
searching dependent graphs…