Running shell command from Python script
<p>I'm trying to run a shell command from within a python script which needs to do several things<br>
1. The shell command is 'hspice tran.deck >! tran.lis'<br>
2. The script should wait for the shell command to complete before proceeding<br>
3. I need to check the return code from the command and<br>
4. Capture STDOUT if it completed successfully else capture STDERR  </p>

<p>I went through the subprocess module and tried out a couple of things but couldn't find a way to do all of the above.<br>
- with subprocess.call() I could check the return code but not capture the output.<br>
- with subprocess.check_output() I could capture the output but not the code.<br>
- with subprocess.Popen() and Popen.communicate(), I could capture STDOUT and STDERR but not the return code.<br>
I'm not sure how to use Popen.wait() or the returncode attribute. I also couldn't get Popen to accept '>!' or '|' as arguments.  </p>

<p>Can someone please point me in the right direction? I'm using Python 2.7.1  </p>

<p>EDIT: Got things working with the following code  </p>

<pre><code>process = subprocess.Popen('ls | tee out.txt', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if(process.returncode==0):
  print out
else:
  print err
</code></pre>

<p>Also, should I use a <em>process.wait()</em> after the <em>process = line</em> or does it wait by default?</p>
