title: Python: Using subprocess to give input to another program
body: <p>I am trying to use Python to automate running another program repeatedly.  Right now I am typing the following into the command line, one at a time (the numbers come from the file "Avals"):</p>
  
  <pre><code>advisersProgram
  input1
  0.01
  input1
  0.015
  exit
  </code></pre>
  
  <p>When I try to automate it, I can launch advisersProgram but I can't send it the inputs.</p>
  
  <p>This is what I have tried:</p>
  
  <pre><code>import os
  import glob
  import subprocess
  
  files = sorted(glob.glob("*"))
  for f in files:
      os.chdir(f)
      As = [float(line.strip()) for line in open("Avals")]
      subprocess.call('advisersProgram')
      for A in As:
          subprocess.call('input1')
          subprocess.call(A)
      subprocess.call('exit')
      os.chdir("..")
  </code></pre>
  
  <p>I have also tried </p>
  
  <pre><code>for f in files:
      As = [float(line.strip()) for line in open("Avals")]
      subprocess.call(['advisersProgram','input1',A[0],'input1,A[1]','exit'])
  </code></pre>
  
  <p>and</p>
  
  <pre><code>for f in files:
      As = [float(line.strip()) for line in open("Avals")]
      subprocess.Popen('advisersProgram',stdin=['input1','0.01','input1','0.015','exit'])
  </code></pre>
  
  <p>Other info:
  I looked into Pexpect (I am not sure if this would be useful but it was suggested in one of the stack exchange answers I read), but I do not have that installed and do not have the authority to install it.</p>
  
  <p>I don't need to capture any output; advisersProgram generates contour plots which are saved in the directory.</p>