title: Python redirection in subprocess.Popen
body: <p>I want to use a bash command using python's <code>subprocess.Popen</code>.
  My bash command looks like:</p>
  <pre><code>$ gunzip -c /my/dir/file1.gz /my/dir/file2.gz | gsplit -l 500000 --numerical-suffixes=1 --suffix-length=3 --additional-suffix=.split - /my/dir/output/file_
  </code></pre>
  <p>It takes compressed files, uncompresses them, merges the content, splits the content into output files. I can do that in Python this way:</p>
  <pre><code>from __future__ import print_function
  import subprocess
  dir = &quot;/my/dir&quot;
  files = [&quot;file1.gz&quot;, &quot;file2.gz&quot;]
   
  cmd1 = &quot;gunzip -c {}&quot;.format(&quot; &quot;.join([dir+files[0], dir+files[1]]))
  cmd2 = &quot;{} -l {} --numeric-suffixes={} --suffix-length={} --additional-suffix={}  - {}&quot;\
          .format(&quot;gsplit&quot;, 500000, 1, 3, &quot;.split&quot;#, &quot;'gzip &gt; $FILE.gz'&quot;
                  , &quot;/my/dir/output/file_&quot;)
    
  proc1 = subprocess.Popen(str(cmd1).split(), stdout=subprocess.PIPE)
  proc2 = subprocess.Popen(str(cmd2).split(), stdin=proc1.stdout, stdout=subprocess.PIPE)
  
  proc1.stdout.close()
  proc2.wait()
  print(&quot;result:&quot;, proc2.returncode)
  </code></pre>
  <p>Then I can check the output:</p>
  <pre><code>$ ls /my/dir/output
  file_001.split
  file_002.split
  file_003.split
  </code></pre>
  <p>Now I want to make use of the gsplit's  <code>--filter</code> argument, which allows to pipe the result to another command. Here, I chose gzip as I want to compress the output. Bash command looks like this:</p>
  <pre><code>$ gunzip -c /my/dir/file1.gz /my/dir/file2.gz | gsplit -l 500000 --numerical-suffixes=1 --suffix-length=3 --additional-suffix=.split --filter='gzip &gt; $FILE.gz' - /my/dir/output/file_
  </code></pre>
  <p>This command works.</p>
  <p>Now putting it into python code:</p>
  <pre><code>from __future__ import print_function
  import subprocess
  dir = &quot;/my/dir&quot;
  files = [&quot;file1.gz&quot;, &quot;file2.gz&quot;]
   
  cmd1 = &quot;gunzip -c {}&quot;.format(&quot; &quot;.join([dir+files[0], dir+files[1]]))
  cmd2 = &quot;{} -l {} --numeric-suffixes={} --suffix-length={} --additional-suffix={}  --filter={} - {}&quot;\
          .format(&quot;gsplit&quot;, 500000, 1, 3, &quot;.split&quot;, &quot;'gzip &gt; $FILE.gz'&quot;
                  , &quot;/my/dir/output/file_&quot;)
    
  proc1 = subprocess.Popen(str(cmd1).split(), stdout=subprocess.PIPE)
  proc2 = subprocess.Popen(str(cmd2).split(), stdin=proc1.stdout, stdout=subprocess.PIPE)
  
  proc1.stdout.close()
  proc2.wait()
  print(&quot;result:&quot;, proc2.returncode)
  </code></pre>
  <p>Alas I get this error:</p>
  <blockquote>
  <p>/usr/local/bin/gsplit: invalid option -- 'f'</p>
  <p>Try '/usr/local/bin/gsplit --help' for more information.</p>
  <p>gunzip: error writing to output: Broken pipe</p>
  <p>gunzip: /my/dir/file1.gz: uncompress failed</p>
  <p>gunzip: error writing to output: Broken pipe</p>
  <p>gunzip: /my/dir/file12.gz: uncompress failed</p>
  </blockquote>
  <p>I think it has to do with the redirection symbol in <code>gzip &gt; $FILE.gz</code>.</p>
  <p>What is going on, how can I resolve this issue?</p>