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>I would handle the file in Python:</p>

<pre><code>link = "stage2output"
subprocess.call(["perl", "run_esearch.pl", filename, "result"])
with open(link, "w") as f:
    subprocess.call(["perl", "shrink.pl", "result"], stdout=f)
subprocess.call(["perl", "shrink2.pl", link])
</code></pre>

<p>On the off-chance that <code>shrink2.pl</code> can take a filename of <code>-</code> to read from standard input:</p>

<pre><code>subprocess.call(["perl", "run_esearch.pl", filename, "result"])
p2 = subprocess.Popen(["perl", "shrink.pl", "result"], stdout=subprocess.PIPE)
subprocess.call(["perl", "shrink2.pl", "-"], stdin=p2.stdin)
</code></pre>

python bash perl subprocess stdout