title: Execute bash script from Python on Windows
body: <p>I am trying to write a python script that will execute a bash script I have on my Windows machine. Up until now I have been using the Cygwin terminal so executing the bash script RunModels.scr has been as easy as <code>./RunModels.scr</code>. Now I want to be able to utilize <code>subprocess</code> of Python, but because Windows doesn't have the built in functionality to handle bash I'm not sure what to do.</p>
  
  <p>I am trying to emulate <code>./RunModels.scr &lt; validationInput &gt; validationOutput</code></p>
  
  <p>I originally wrote this:</p>
  
  <pre><code>os.chdir(atm)
  vin = open("validationInput", 'r')
  vout = open("validationOutput", 'w')
  subprocess.call(['./RunModels.scr'], stdin=vin, stdout=vout, shell=True)
  vin.close()
  vout.close()
  os.chdir(home)
  </code></pre>
  
  <p>But after spending a while trying to figure out why my access was denied, I realized my issue wasn't the file permissions but the fact that I was trying to execute a bash file on Windows in general. Can someone please explain how to execute a bash script with directed input/output on windows using a python script?</p>
  
  <p>Edit <strong>(Follow up Question)</strong>:</p>
  
  <p>Thanks for the responses, I needed the full path to my bash.exe as the first param. Now however, command line calls from within RunModels.scr come back in the python output as <code>command not found</code>. For example, <code>ls</code>, <code>cp</code>, <code>make</code>. Any suggestions for this?</p>
  
  <p><strong><em>Follow up #2</em></strong>:
  I updated my call to this: </p>
  
  <p><code>subprocess.call(['C:\\cygwin64\\bin\\bash.exe', '-l', 'RunModels.scr'], stdin=vin, stdout=vout, cwd='C:\\path\\dir_where_RunModels\\')</code></p>
  
  <p>The error I now get is <code>/usr/bin/bash: RunModels.scr: No such file or directory</code>.
  Using cwd does not seem to have any effect on this error, either way the subprocess is looking in <code>/usr/bin/bash</code> for <code>RunModels.scr</code>. </p>
  
  <p><strong><em>SELF-ANSWERED</em></strong>
  I needed to specify the path to RunModels.scr in the call as well as using <code>cwd</code>.</p>
  
  <p><code>subprocess.call(['C:\\cygwin64\\bin\\bash.exe', '-l', 'C:\\path\\dir_where_RunModels\\RunModels.scr'], stdin=vin, stdout=vout, cwd='C:\\path\\dir_where_RunModels\\')</code></p>
  
  <p><strong><em>But another problem...</em></strong></p>
  
  <p>Regardless of specifying <code>cwd</code>, the commands executed by <code>RunModels.scr</code> are throwing errors as if <code>RunModels.scr</code> is in the wrong directory. The script executes, but <code>cp</code> and <code>cd</code> throw the error <code>no such file or directory</code>. If I navigate to where <code>RunModels.scr</code> is through the command line and execute it the old fashioned way I don't get these errors.</p>