Python error: subprocess.CalledProcessError: Command returned non-zero exit status 1
<p>I need to count the lines of a shell-command output in python script.  </p>

<p>This function works fine in case there is output, but in case the output is empty, it gives an error as explained in the error output.<br>
I tried to avoid that using an <code>if</code> statement in case the output of the command is <code>None</code>, but that didn't help.</p>

<pre><code>#!/usr/bin/python
import subprocess

lines_counter=0
func="nova list | grep Shutdown "
data=subprocess.check_output(func, shell=True)
if data is True:
       for line in data.splitlines():
               lines_counter +=1
print lines_counter
</code></pre>

<p>Error output:</p>

<pre><code>data=subprocess.check_output(func, shell=True)
  File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'nova list | grep Shutdown ' returned non-zero exit status 1
</code></pre>