Building a Python script which runs Perl scripts. How to redirect stdout?
<p>I am writing a Python script that use some Perl scripts, but one of them uses stdout so I must use a redirection <code>&gt;</code> in bash to write this output to a file.</p>

<p>All input and output files are text files.</p>

<pre><code># -*- coding: utf-8 -*-
import subprocess 

filename = input("What the name of the input? ")

#STAGE 1----------------------------------------------------------------------
subprocess.Popen(["perl", "run_esearch.pl", filename , 'result'])

#STAGE 2----------------------------------------------------------------------
subprocess.Popen(["perl", "shrink.pl", 'result']) 
'''Here the input from stage one is "shrunk" to smaller file, but
the output is printed to the console. Is it possible to write this out
to a file in Python, so I can use it in stage 3? '''

#STAGE 3----------------------------------------------------------------------
subprocess.Popen(["perl", "shrink2.pl", 'stdoutfromstage2'])
</code></pre>

<p>Here is an example of how you can use <code>bash</code> to redirect output to a file <code>test.txt</code>:</p>

<pre><code>import subprocess 

#STAGE 2----------------------------------------------------------------------
subprocess.Popen(['bash', '-c', 'echo Hello &gt; test.txt']) 

#STAGE 3----------------------------------------------------------------------
subprocess.Popen(['perl', '-nE', 'say $_', 'test.txt'])
</code></pre>

python bash perl subprocess stdout