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.

5
  • 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 Commented Nov 10, 2012 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? Commented Nov 11, 2012 at 0:24
  • both work, but when the exe opens its simply sticks and no output is generated. Commented Nov 11, 2012 at 14:22
  • btw, cc should also use raw-string literals i.e., r'c:\MM...', not 'c:\MM...' Commented Nov 11, 2012 at 14:38
  • "sticks" might mean the the exe waits for input from stdin. Commented Nov 11, 2012 at 14:40

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

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: update your question 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".
@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.
@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: I've added link to SendKeys example that might work in your case.
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)

3 Comments

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?
@J.F.Sebastian: _gethandles 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.
@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???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.