title: Why is subprocess.run output different from shell output of same command?
body: <p>I am using <code>subprocess.run()</code> for some automated testing. Mostly to automate doing:</p>
  
  <pre><code>dummy.exe &lt; file.txt &gt; foo.txt
  diff file.txt foo.txt
  </code></pre>
  
  <p>If you execute the above redirection in a shell, the two files are always identical. But whenever <code>file.txt</code> is too long, the below Python code does not return the correct result.</p>
  
  <p>This is the Python code:</p>
  
  <pre class="lang-python3 prettyprint-override"><code>import subprocess
  import sys
  
  
  def main(argv):
  
      exe_path = r'dummy.exe'
      file_path = r'file.txt'
  
      with open(file_path, 'r') as test_file:
          stdin = test_file.read().strip()
          p = subprocess.run([exe_path], input=stdin, stdout=subprocess.PIPE, universal_newlines=True)
          out = p.stdout.strip()
          err = p.stderr
          if stdin == out:
              print('OK')
          else:
              print('failed: ' + out)
  
  if __name__ == "__main__":
      main(sys.argv[1:])
  </code></pre>
  
  <p>Here is the C++ code in <code>dummy.cc</code>:</p>
  
  <pre class="lang-c++ prettyprint-override"><code>#include &lt;iostream&gt;
  
  
  int main()
  {
      int size, count, a, b;
      std::cin &gt;&gt; size;
      std::cin &gt;&gt; count;
  
      std::cout &lt;&lt; size &lt;&lt; " " &lt;&lt; count &lt;&lt; std::endl;
  
  
      for (int i = 0; i &lt; count; ++i)
      {
          std::cin &gt;&gt; a &gt;&gt; b;
          std::cout &lt;&lt; a &lt;&lt; " " &lt;&lt; b &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p><code>file.txt</code> can be anything like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>1 100000
  0 417
  0 842
  0 919
  ...
  </code></pre>
  
  <p>The second integer on the first line is the number of lines following, hence here <code>file.txt</code> will be 100,001 lines long. </p>
  
  <p><strong>Question:</strong> Am I misusing subprocess.run() ?  </p>
  
  <p><strong>Edit</strong></p>
  
  <p>My exact Python code after comment (newlines,rb) is taken into account:</p>
  
  <pre><code>import subprocess
  import sys
  import os
  
  
  def main(argv):
  
      base_dir = os.path.dirname(__file__)
      exe_path = os.path.join(base_dir, 'dummy.exe')
      file_path = os.path.join(base_dir, 'infile.txt')
      out_path = os.path.join(base_dir, 'outfile.txt')
  
      with open(file_path, 'rb') as test_file:
          stdin = test_file.read().strip()
          p = subprocess.run([exe_path], input=stdin, stdout=subprocess.PIPE)
          out = p.stdout.strip()
          if stdin == out:
              print('OK')
          else:
              with open(out_path, "wb") as text_file:
                  text_file.write(out)
  
  if __name__ == "__main__":
      main(sys.argv[1:])
  </code></pre>
  
  <p>Here is the first diff:</p>
  
  <p><a href="https://i.stack.imgur.com/Fk2IW.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Fk2IW.jpg" alt="enter image description here"></a></p>
  
  <p>Here is the input file: <a href="https://drive.google.com/open?id=0B--mU_EsNUGTR3VKaktvQVNtLTQ" rel="noreferrer">https://drive.google.com/open?id=0B--mU_EsNUGTR3VKaktvQVNtLTQ</a></p>