On 2010-11-18, Tim Harig <user...@ilthio.net> wrote:
> On 2010-11-18, noydb <noyd...@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

Reply via email to