subprocess.CalledProcessError: what *is* the error?
<pre><code>import subprocess

cmd = "grep -r * | grep jquery"
print cmd

subprocess.check_output(cmd, shell=True)
</code></pre>

<p><strong>subprocess.CalledProcessError: Command 'grep -r * | grep jquery' returned non-zero exit status 1</strong></p>

<p>I can execute that command in my shell without issues. How can I see the <strong>actual</strong> error in python?</p>

<p><em>The snippet is part of a larger script which takes multiple arguments and chains the grep commands + adds some excludes (I don't need to grep log files or minified JavaScript. Hence the pedantic syntax.</em></p>

<p><em>Python 2.7.10</em></p>

<p><a href="https://docs.python.org/2/library/subprocess.html" rel="nofollow">Quoting docs</a>:</p>

<blockquote>
  <p>If the return code was non-zero it raises a <code>CalledProcessError</code>. The
  <code>CalledProcessError</code> object will have the return code in the <code>returncode</code>
  attribute and any output in the <code>output</code> attribute.</p>
</blockquote>

<pre><code>import subprocess

cmd = "grep -r * | grep jquery"
print cmd

try:
    subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as e:
    print e.returncode
    print e.output
</code></pre>

<p>Of error message was printed to stderr, you need keyword argument <code>stderr=subprocess.STDOUT</code>.</p>

<p>There is no other source of 'what went wrong' besides return code and stdout / stderr.</p>

python subprocess