How to run an EXE, with argument, capture output value
Tim Harig
usernet at ilthio.net
Thu Nov 18 00:36:59 EST 2010
On 2010-11-18, Tim Harig <usernet at ilthio.net> wrote:
> 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sorry, I missed the second part, it's time for me to go to bed.
>> really doesn't need to be 'seen'? Or, would the user still have to
>> click the 'compute' button?
>
> 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.
> >>>
With capturing the output, it looks like:
>>> pig = subprocess.Popen(["/usr/games/pig"], stdin=subprocess.PIPE,
>>> stdout=subprocess.PIPE)
>>> result = pig.communicate(input=b"This is sample text.\n")[0]
>>> result
b'Isthay isway amplesay exttay.\n'
>>>
You can also get the return code if you need it:
>>> pig.returncode
0
More information about the Python-list
mailing list