[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: How to run an EXE, with argument, capture output value
From:       Tim Harig <usernet () ilthio ! net>
Date:       2010-11-18 5:36:59
Message-ID: ic2e1r$m5c$1 () speranza ! aioe ! org
[Download RAW message or body]

On 2010-11-18, Tim Harig <usernet@ilthio.net> wrote:
> On 2010-11-18, noydb <noydb00@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
-- 
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic