1

I am trying to automate a Windows app that requires input via a GUI using Python 2.7 script. I am calling the exe via the built-in python subprocess functions as follows:

import subprocess

cc= 'C:\MM\test.exe'

subprocess.call(cc)

When the exe is called, a GUI requires that I enter a path manually for the input file, a data.txt file. When I enter the path processing can begin. I would somehow like to automate this process, namely, just call the exe and have it find the input.txt by itself and also importantly, print the output to an output.txt file.

I initially tried the following suggestions:

import subprocess

with open(r'C:\MMA\DATA\input.txt', 'r') as input_file, open(r'C:\MMA\DATA\output.txt', 'w') as output_file: subprocess.call(['C:\MM\test.exe'], stdin=input_file, stdout=output_file)

However, this was unsuccessful; the exe still required that I enter the input file path manually.

I am not sure how to proceed here, I have no experience with this sort of issue, and any help would be very much appreciated. Thank you in advance, paulc.

paulc
  • 455
  • 4
  • 10
  • when the executable runs it will look for an input file (a data set), in other words how do I tell the exe where to look for the data in the context of the subprocess functions and when the process completes the exe will generate a new data set, again how in the context of the subprocess.call function do I set the path for the data to be printed – paulc Nov 10 '12 at 22:01
  • how do you run it manually in cmd.exe: a) `test.exe input.txt output.txt` OR b) `test.exe < input.txt > output.txt`? – jfs Nov 11 '12 at 00:24
  • both work, but when the exe opens its simply sticks and no output is generated. – paulc Nov 11 '12 at 14:22
  • btw, `cc` should also use raw-string literals i.e., `r'c:\MM...'`, not `'c:\MM...'` – jfs Nov 11 '12 at 14:38
  • "sticks" might mean the the exe waits for input from stdin. – jfs Nov 11 '12 at 14:40

2 Answers2

2

If test.exe input.txt output.txt also works when you run it manually from a command-line then it is more robust on Windows to pass input/output files as arguments and not through stdin/stdout redirection:

from subprocess import check_call

inputfn = r'C:\MMA\DATA\input.txt'
outputfn = r'C:\MMA\DATA\output.txt'
check_call([r'C:\MM\test.exe', inputfn, outputfn], close_fds=True)

If test.exe is a GUI program then you could try SendKeys module (see the last example)

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thanks very much for the above input J.F. Sebastain, same situation here, the exe opens (GUI opens) and waits for input, I have to enter the path of the input file manually before processing can continue? I would idealy like to automate this, I would imagine this is poosible in Windows? – paulc Nov 11 '12 at 15:05
  • @paulc: [update your question](http://stackoverflow.com/posts/13326418/edit) to include information on how you run test.exe (manually from a command-line (cmd.exe)) *successfully*. To avoid confusion: if `test.exe input.txt output.txt` "sticks", doesn't produce the required output in output.txt file, etc; it means "unsuccessful". – jfs Nov 11 '12 at 15:18
  • @paulc: Windows GUI apps rarely have an attached console, which rules out using stdin/stdout, and often they don't even touch the command line (it's just an unparsed string in WinMain). I get the feeling by 'sticks' you mean it opens a file open dialog, and you're looking for a GUI automation solution. – Eryk Sun Nov 11 '12 at 15:25
  • @J.F Sebastain and eryksun, thanks guys, I will update the question, as pointed out by eryksun the "sticking" situation could be best described as a file open dialog, and in that case I am indeed looking for a GUI automation solution. – paulc Nov 11 '12 at 16:12
  • @paulc: I've added link to SendKeys example that might work in your case. – jfs Nov 11 '12 at 16:19
1

The documentation of the subprocess.call function provides you with the correct syntax for redirecting the standard input/output of the called process from/to a file:

import subprocess

with open(r'C:\MMA\DATA\input.txt', 'r') as input_file, open(r'C:\MMA\DATA\output.txt', 'w') as output_file:
    subprocess.call(['C:\MM\test.exe'], stdin=input_file, stdout=output_file)
Pedro Romano
  • 10,973
  • 4
  • 46
  • 50
  • Does it handle newlines correctly? Do you see any difference if you specify `'rb'`, `'wb'` instead of 'r', 'w' for a simple C program that writes to output.txt number of bytes read from input.txt? – jfs Nov 11 '12 at 00:32
  • @J.F.Sebastian: [`_gethandles`](http://hg.python.org/cpython/file/70274d53c1dd/Lib/subprocess.py#l760) creates inheritable duplicates of the low-level file handles, which are set in the `STARTUPINFO` structure. I just tested the output of `dir` with `shell=True`. Opening the stdout file with 'w' vs 'wb' made no difference. – Eryk Sun Nov 11 '12 at 09:16
  • @Pedro: Just awake ...Thanks Pedro for your suggestion, the python code opens the exe, but in some nonfunctional manner?? i.e. no output is generated...not sure what to do??? – paulc Nov 11 '12 at 14:25