How to run an EXE, with argument, capture output value

Tim Harig usernet at ilthio.net
Thu Nov 18 06:24:45 CET 2010


On 2010-11-18, noydb <noydb00 at gmail.com> wrote:
> I have an executable that I want to run within python code.  The exe
> requires an input text file, the user to click a 'compute' button, and
> then the exe calculates several output values, one of which I want to
> capture into a variable.  Can I use Python to supply the input file,
> execute the exe and capture the output value, like such that the exe
> really doesn't need to be 'seen'?  Or, would the user still have to
> click the 'compute' button?
>
> Any code snippets or guidance would be very much appreciated.  I have
> found that
>
> import os
> os.system('C:\xTool\stats_hall.exe')
>
> will run the exe.  And, maybe these execl/execle/execlp/etc functions
> might be what I need for adding in the argument, but documentation
> seems to indicate that these do not return output.  ??

If you are not already, I would highly suggest using Python3 with the
subprocess module:

	http://docs.python.org/py3k/library/subprocess.html

It puts everything in one place and supercedes the exec* functions which
where a PITA.  You can 95% of what you need simply using
subprocess.Popen().  There are several examples from this group in the past
few days; but, the process looks something like this:

	Python 3.1.2 (r312:79147, Oct  9 2010, 00:16:06)
	[GCC 4.4.4] on linux2
	Type "help", "copyright", "credits" or "license" for more information.
	>>> import subprocess
	>>> pig = subprocess.Popen(["/usr/games/pig"], stdin=subprocess.PIPE)
	>>> result = pig.communicate(input=b"This is sample text.\n")
	Isthay isway amplesay exttay.
	>>>



More information about the Python-list mailing list