subprocess problem

eryk sun eryksun at gmail.com
Thu Feb 9 19:03:53 EST 2017


On Thu, Feb 9, 2017 at 10:50 PM, Cameron Simpson <cs at zip.com.au> wrote:
> This is why I suggested the check_returncode() method, which examines the
> error code.

You must be thinking of the returncode attribute, which isn't a
method. check_returncode() is a method of the CompletedProcess object
that's returned by subprocess.run(), which was added in 3.5. The OP is
using both 3.4 and 3.5, so run() isn't a practical option.

The older check_output() function also checks the return code and
raises a subprocess.CalledProcessError if the command fails. For
example:

    >>> subprocess.check_output(['which', 'ls'])
    b'/bin/ls\n'

    >>> subprocess.check_output(['which', 'pandoc'])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.5/subprocess.py", line 626, in check_output
        **kwargs).stdout
      File "/usr/lib/python3.5/subprocess.py", line 708, in run
        output=stdout, stderr=stderr)
    subprocess.CalledProcessError: Command '['which', 'pandoc']'
    returned non-zero exit status 1



More information about the Python-list mailing list