I have a executable, called tsfoil2.exe, and I want to operate this .exe from my python environment. I'm running Python 2.7.3, with Spyder 2.1.11 on Windows 7.
In order to operate the .exe, it requires some input, the default hard drive ('I:\'), a name for the outputfile ('test'), and a name for the input file ('SC20610.inp').
One of my colleagues advised me to use os.system, and supply this with a temporary input file, that contains all the arguments.
f = open('temp', 'w')
f.write('I:\ \n')
f.write('test \n')
f.write('SC20610.inp\n')
f.close()
I then supply this file with arguments to the .exe in the following way:
os.system("tsfoil2.exe < temp")
This all works, but the program requires a 'ENTER' to close. For some reason, the .exe is repeatedly asking to 'Press the ENTER key to exit'. Even, when I press the enter key in my Spyder-console, the program does not terminate.
Is there a way to give the 'ENTER' key as an interactive input to .exe? I've tried to use the SendKeys class, but as the program does not terminate, it does not reach the line of code that contains the SendKeys command. I've also tried to include it in the arguments-file, but this does not work either.
Furthermore I've also found out that it might be beneficial to switch to subprocesses command, as it might give me more command over the execution, but I haven't been able to run the executable with the input files.
Is it possible to provide the necessary 'ENTER' using os.system, or should I switch to subprocess, and if so, how can I construct a method similar to the os.system("tsfoil2.exe < temp") I'm using now.
I've tried this:
import subprocess as sub
f = open('temp', 'w')
f.write('I:\ \n')
f.write('test \n')
f.write('SC20610.inp\n')
f.close()
proc=sub.Popen(["tsfoil2.exe","temp"], shell=True)
and this
import subprocess as sub
p=sub.Popen('tsfoil2.exe')
p.communicate(input='I:' )
But, the program does not respond to the arguments given.
MWE:
import os
f = open('temp', 'w')
f.write('I:\ \n')
f.write('test \n')
f.write('SC20610.inp\n')
f.close()
os.system("tsfoil2.exe < temp")
Both the program can be found via http://www.dept.aoe.vt.edu/~mason/Mason_f/tsfoil2.exe, the input file can be found via http://www.dept.aoe.vt.edu/~mason/Mason_f/SC20610.inp.
I hope everything is clear, and you can help me out.