title: Simple Windows example of automating a .exe program using subprocess and .Popen
body: <p>I am new to the subprocess module, and after reading the <a href="http://docs.python.org/2/library/subprocess.html" rel="nofollow">Python documents</a> among many other websites (including Stack Overflow), I am struggling to find simplified examples of <code>.Popen</code>, <code>.communicate</code>, and other such useful classes. Often, the examples never use each class in succession, only on their own. Also, many examples are Linux-based, such as the use of <code>["ls", "-l"]</code>, which makes it quite difficult to understand for Windows users.</p>
  
  <p>After several hours playing with this module, I have encountered several issues, which could be best illustrated through the method of opening and communicating with a simple .exe command line program.</p>
  
  <p>For example, say that the program is called "numbers.exe", and that it asks the following questions:</p>
  
  <pre><code>&gt;&gt;&gt; Question 1) Choose 1, 2 or 3
  &gt;&gt;&gt; Question 2) Choose 4, 5 or 6
  &gt;&gt;&gt; You have answered #(Q1) and #(Q2)
  &gt;&gt;&gt; *Use these values in an iterative sequence displaying each stage in the iteration*
  </code></pre>
  
  <p>I then want to manipulate this program automatically, i.e. I want python to input 2 and 6 without me having to do anything, but still print the questions. I then want to be able to view the iteration in python.</p>
  
  <p>The first consideration here is that I could use:</p>
  
  <pre><code>from subprocess import Popen, PIPE
  
  numprog = subprocess.call('numbers.exe')
  print(numprog.communicate())
  </code></pre>
  
  <p>However, this just opens the program, and I would still have to enter 2 and 6 myself. To automate the process, I believe I must use <code>Popen</code>, alongside stdin, stdout and stderr. This is where I encounter issues. I understand I must use Popen to begin communicating with the input (stdin), output (stdout) and error (stderr) pipes:</p>
  
  <pre><code>from subprocess import Popen, PIPE
  
  numcomms = Popen('numbers.exe', stdout=PIPE, stdin=PIPE, stderr=PIPE)
  </code></pre>
  
  <p>I am unsure what to do from here. Using <code>numcomms.stdout.read()</code> causes the program to just linger, and using <code>numcomms.stdin.write(2)</code> throws out an error that int values cannot be used. The <code>numprog.communicate</code> class seems to require you to enter the values yourself.</p>
  
  <p>From what I can see, the pseudo code would be along the lines:</p>
  
  <pre><code>&gt;&gt;&gt; Open numbers.exe stdin, stdout and stderr pipes using Popen
  &gt;&gt;&gt; Print first question using stdout
  &gt;&gt;&gt; Enter "2" using stdin
  &gt;&gt;&gt; Print second question using stdout
  &gt;&gt;&gt; Enter "6" using stdin
  &gt;&gt;&gt; Receive a string saying "You have answered 2 and 6" using stdout
  &gt;&gt;&gt; Display the results of each stage of the iteration using stdout
  </code></pre>
  
  <p>How would I go about writing this?</p>
  
  <p>Really appreciate the help, thank you!</p>
  
  <p><strong>Edit</strong>: Edited the question to describe the iterative sequence issue. Michael has suggested a good resolution to the inputting issue, however I am having trouble printing the results of the iteration.</p>