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

Jean-Michel Pichavant jeanmichel at
Thu Nov 18 12:02:04 CET 2010

Tim Harig wrote:
> On 2010-11-18, noydb <noydb00 at> 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:
> 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.
> 	>>>
Suggesting subprocess is a good idea, *highly* suggesting python3 is 
questionable. The above code works in python 2. Many libraries (those 
included batteries) have not been ported yet to python 3.
Py3 is a better core language than py2, but for now, less featured.


More information about the Python-list mailing list