<p>How do I execute the following shell command using the Python <a href="https://docs.python.org/library/subprocess.html" rel="noreferrer"><code>subprocess</code></a> module?</p>

<pre><code>echo "input data" | awk -f script.awk | sort &gt; outfile.txt
</code></pre>

<p>The input data will come from a string, so I don't actually need <code>echo</code>.  I've got this far, can anyone explain how I get it to pipe through <code>sort</code> too?</p>

<pre><code>p_awk = subprocess.Popen(["awk","-f","script.awk"],
                          stdin=subprocess.PIPE,
                          stdout=file("outfile.txt", "w"))
p_awk.communicate( "input data" )
</code></pre>

<p><strong>UPDATE</strong>:  Note that while the accepted answer below doesn't actually answer the question as asked, I believe S.Lott is right and it's better to avoid having to solve that problem in the first place!</p>

http://www.python.org/doc/2.5.2/lib/node535.html
covered this pretty well. Is there some part of this you didn't understand?
Your program would be pretty similar, but the second
```
Popen
```
would have stdout= to a file, and you wouldn't need the output of its
```
.communicate()
```
.