title: Python subprocess cannot capture output of Windows program
body: <p>Using <code>stdout=subprocess.PIPE</code> stops the output from going to the console, but nothing is captured.</p>
  
  <pre><code>&gt;&gt;&gt; import subprocess
  &gt;&gt;&gt; proc = subprocess.Popen(['C:\\Users\\me\\program.exe'])
  &gt;&gt;&gt; ERROR: please provide an argument
  // TRUNCATED USAGE OUTPUT
  proc.wait()
  0
  &gt;&gt;&gt; proc = subprocess.Popen([''C:\\Users\\me\\program.exe''], stdout=subprocess.PIPE)
  &gt;&gt;&gt; proc.communicate()
  ('', None)
  </code></pre>
  
  <p>I've tried every combination available on stackoverflow. <code>shell=True</code> hasn't worked. Spawning a sub <code>cmd</code> hasn't worked. <code>subprocess.check_output</code> captures nothing. I'm happy to retry any of these commands in the comments. </p>
  
  <p>I am guessing this has something to do with out the program is attaching to a shell.</p>
  
  <p>This is the assembly the program uses to output (<code>mcall</code> is just a macro to align memory to 16 bits). The reason I include this is in case <code>GetStdHandle</code> is affecting things.</p>
  
  <pre><code>console_write PROC
  ; rcx   MSG
  ; rdx   LEN
    prologue
    push    rcx
    push    rdx
    xor     rcx, rcx
    mov     ecx, [stdout]
    mcall   GetStdHandle
    mov     rcx, rax
    xor     rdx, rdx
    pop     r8    ; len
    pop     rdx   ; msg
    push    0
    mov     r9, rsp
    push    0
    mcall   WriteConsoleA
    pop     rcx
    pop     rcx
    epilogue
  console_write ENDP
  </code></pre>
  
  <p>I'm stumped on this one. I've done this so many times on Linux. I just don't know enough about Windows internals to sort this out. Thanks!</p>
  
  <hr>
  
  <p><strong>Edit: Additional things I have tried:</strong></p>
  
  <p>Admin (also with STDERR capture)</p>
  
  <pre><code>C:\Windows\system32&gt;C:\Python27\python.exe
  Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import subprocess
  &gt;&gt;&gt; proc = subprocess.Popen(['C:\\Users\\me\\program.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  &gt;&gt;&gt; proc.communicate()
  ('', '')
  </code></pre>
  
  <p>STDOUT File Redirect</p>
  
  <pre><code>&gt;&gt;&gt; import os
  &gt;&gt;&gt; os.system('C:\\Users\\me\\program.exe &gt; out.txt')
  0
  &gt;&gt;&gt; f = open('out.txt', 'r')
  &gt;&gt;&gt; f.read()
  ''
  </code></pre>
  
  <p>STDERR File Redirect</p>
  
  <pre><code>&gt;&gt;&gt; import os
  &gt;&gt;&gt; os.system('C:\\Users\\me\\program.exe 2&gt;out.txt')
  ERROR: please provide an argument
  // TRUNCATED USAGE OUTPUT
  0
  &gt;&gt;&gt; open('out.txt', 'r').read()
  ''
  </code></pre>
  
  <p>Command Line Capture Fails (suppresses output from going to cmd, but nothing is captured)</p>
  
  <pre><code>program.exe &gt; out.txt
  </code></pre>