Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to run an EXE, with argument, capture output value (http://www.velocityreviews.com/forums/t738279-how-to-run-an-exe-with-argument-capture-output-value.html)

noydb 11-18-2010 03:47 AM

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

I would appreciate some guidance on this. I'm a newbe, sorry if I
sound dumb - I kind of am on this stuff!

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. ??

Thanks much.

Tim Harig 11-18-2010 05:24 AM

Re: How to run an EXE, with argument, capture output value
 
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
> 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.
>>>

Tim Harig 11-18-2010 05:36 AM

Re: How to run an EXE, with argument, capture output value
 
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

Jean-Michel Pichavant 11-18-2010 11:02 AM

Re: How to run an EXE, with argument, capture output value
 
Tim Harig 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
>> 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.
> >>>
>

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.

JM

noydb 11-18-2010 08:15 PM

Re: How to run an EXE, with argument, capture output value
 
I will use 2.5.

I tried your suggestion, with this code

import subprocess
pig = subprocess.Popen(["C:\Halls\hallbig2.exe"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
result = pig.communicate(input='C:\Halls\Input\Ea39j.txt')[-1] #I need
to capture the, what I think is the, last output
print result
print pig.returncode
>> None
>> 0


So the tuple is empty. ?? The exe executes fine and returns output in
th exe tool itself. The python script seems to execute fine, no
errors, '...returned exit code 0'. Any ideas/suggestions?

Tim Harig 11-18-2010 10:22 PM

Re: How to run an EXE, with argument, capture output value
 
On 2010-11-18, noydb <jenn.duerr@gmail.com> wrote:
> import subprocess
> pig = subprocess.Popen(["C:\Halls\hallbig2.exe"],
> stdin=subprocess.PIPE, stdout=subprocess.PIPE)
> result = pig.communicate(input='C:\Halls\Input\Ea39j.txt')[-1] #I need
> to capture the, what I think is the, last output


From the subprocess documentation:

[62]communicate() returns a tuple (stdoutdata, stderrdata).
Note that if you want to send data to the process's stdin,
you need to create the Popen object with stdin=PIPE. Similarly,
to get anything other than None in the result tuple, you need
to give stdout=PIPE and/or stderr=PIPE too.

By using index [-1] you are accessing the processes stderr stream. I am
not really sure why you changed it. It doesn't give you the last output.
Index 0 gives you *all* of stdout and index 1 gives you *all* of stderr,
period. If you wish to further disect the output to get say the last line,
then you will need to parse it separately.

> print result
> print pig.returncode
>>> None
>>> 0

>
> So the tuple is empty. ?? The exe executes fine and returns output in
> th exe tool itself. The python script seems to execute fine, no
> errors, '...returned exit code 0'. Any ideas/suggestions?


No the tuple contains two items (stdout, stderr). The first is what the
program printed to its stdout stream (which is most likely the output you
see if you run the command at a terminal/console). The second is what it printed to its
stderr stream which is a channel used for out of band data such as error or
status messages. In this case, it is None, because you did open stderr as a
subprocess.PIPE.

Tim Harig 11-18-2010 10:36 PM

Re: How to run an EXE, with argument, capture output value
 
On 2010-11-18, Jean-Michel Pichavant <jeanmichel@sequans.com> wrote:
> Tim Harig wrote:
>> If you are not already, I would highly suggest using Python3 with the

^^^^
>> subprocess module:

> 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.


I didn't, and I don't, recommend Python3 over Python2 for just any
purpose.I recommended Python3's subprocess module over the Python2's
subprocess module if that is indeed possible.

I happen to be one of those that feels this transition was terribly
mis-handled and that there should have been provisions to allow both
versions to either be maintained together or to allow modules from both
versions to work together. I don't really have a huge preference for
either version; but, having to deal with both of them has given the
project a black eye.

noydb 11-18-2010 10:54 PM

Re: How to run an EXE, with argument, capture output value
 
On Nov 18, 5:22*pm, Tim Harig <user...@ilthio.net> wrote:
> On 2010-11-18, noydb <jenn.du...@gmail.com> wrote:
>
> > import subprocess
> > pig = subprocess.Popen(["C:\Halls\hallbig2.exe"],
> > stdin=subprocess.PIPE, stdout=subprocess.PIPE)
> > result = pig.communicate(input='C:\Halls\Input\Ea39j.txt')[-1] #I need
> > to capture the, what I think is the, last output

>
> From the subprocess documentation:
>
> * * * * * *[62]communicate() returns a tuple (stdoutdata, stderrdata).
> * * * * * *Note that if you want to send data to the process's stdin,
> * * * * * *you need to create the Popen object with stdin=PIPE. Similarly,
> * * * * * *to get anything other than None in the result tuple, you need
> * * * * * *to give stdout=PIPE and/or stderr=PIPE too.
>
> By using index [-1] you are accessing the processes stderr stream. *I am
> not really sure why you changed it. *It doesn't give you the last output.
> Index 0 gives you *all* of stdout and index 1 gives you *all* of stderr,
> period. *If you wish to further disect the output to get say the last line,
> then you will need to parse it separately.


Okay, I see now. I did run it to start with 0 -- still same result no
matter if 0 or -1.
So, what is result (stdout, using [0]) in this case? (yes, i know I
sound dumb - programming is not my background, obviously). A list,
tuple??? How do you access stdout (or is it stdoutdata?) results? I
have tried, get errors with all attempts. The exe gui returns several
statistical values uopn inputing a text file (containing numerous
lines of <value> <frequency>) and clicking compute - I want just one
of the values.

>
> > print result
> > print pig.returncode
> >>> None
> >>> 0

>
> > So the tuple is empty. ?? *The exe executes fine and returns output in
> > th exe tool itself. *The python script seems to execute fine, no
> > errors, '...returned exit code 0'. *Any ideas/suggestions?

>
> No the tuple contains two items (stdout, stderr). *The first is what the
> program printed to its stdout stream (which is most likely the output you
> see if you run the command at a terminal/console). *The second is what it printed to its
> stderr stream which is a channel used for out of band data such as error or
> status messages. *In this case, it is None, because you did open stderr as a
> subprocess.PIPE.



Tim Harig 11-18-2010 11:16 PM

Re: How to run an EXE, with argument, capture output value
 
On 2010-11-18, noydb <noydb00@gmail.com> wrote:
> On Nov 18, 5:22*pm, Tim Harig <user...@ilthio.net> wrote:
>> On 2010-11-18, noydb <jenn.du...@gmail.com> wrote:

> Okay, I see now. I did run it to start with 0 -- still same result no
> matter if 0 or -1.
> So, what is result (stdout, using [0]) in this case? (yes, i know I
> sound dumb - programming is not my background, obviously). A list,


Nope, this one is my fault, I never should have posted being so tired. I
was reading posts after being unable to sleep and missed something
important about what you said. Sorry about the confusion.

> tuple??? How do you access stdout (or is it stdoutdata?) results? I
> have tried, get errors with all attempts. The exe gui returns several


GUI!!!! Ewwwww. I missed that part. GUIs, on Windows, do not have the
standard streams. GUIs are in general, ugly to automate through the GUI
itself. I would be much better if the program can be run with command line
options, text interface, or if provides an automation object through COM or
..Net.

As workaround, if you run Python through Windows Script Host, you can open
the program with WshShell and automate it, by sending it the keystrokes as
you perform the action by typing, with SendKeys()

noydb 11-18-2010 11:20 PM

Re: How to run an EXE, with argument, capture output value
 
On Nov 18, 5:54*pm, noydb <noyd...@gmail.com> wrote:
> On Nov 18, 5:22*pm, Tim Harig <user...@ilthio.net> wrote:
>
>
>
>
>
> > On 2010-11-18, noydb <jenn.du...@gmail.com> wrote:

>
> > > import subprocess
> > > pig = subprocess.Popen(["C:\Halls\hallbig2.exe"],
> > > stdin=subprocess.PIPE, stdout=subprocess.PIPE)
> > > result = pig.communicate(input='C:\Halls\Input\Ea39j.txt')[-1] #I need
> > > to capture the, what I think is the, last output

>
> > From the subprocess documentation:

>
> > * * * * * *[62]communicate() returns a tuple (stdoutdata, stderrdata).
> > * * * * * *Note that if you want to send data to the process's stdin,
> > * * * * * *you need to create the Popen object with stdin=PIPE. Similarly,
> > * * * * * *to get anything other than None in the result tuple, you need
> > * * * * * *to give stdout=PIPE and/or stderr=PIPE too.

>
> > By using index [-1] you are accessing the processes stderr stream. *I am
> > not really sure why you changed it. *It doesn't give you the last output.
> > Index 0 gives you *all* of stdout and index 1 gives you *all* of stderr,
> > period. *If you wish to further disect the output to get say the last line,
> > then you will need to parse it separately.

>
> Okay, I see now. *I did run it to start with 0 -- still same result no
> matter if 0 or -1.
> So, what is result (stdout, using [0]) in this case? *(yes, i know I
> sound dumb - programming is not my background, obviously). *A list,
> tuple??? *How do you access stdout (or is it stdoutdata?) results? *I
> have tried, get errors with all attempts. *The exe gui returns several
> statistical values uopn inputing a text file (containing numerous
> lines of <value> <frequency>) and clicking compute - I want just one
> of the values.
>
>
>
>
>
> > > print result
> > > print pig.returncode
> > >>> None
> > >>> 0

>
> > > So the tuple is empty. ?? *The exe executes fine and returns output in
> > > th exe tool itself. *The python script seems to execute fine, no
> > > errors, '...returned exit code 0'. *Any ideas/suggestions?

>
> > No the tuple contains two items (stdout, stderr). *The first is what the
> > program printed to its stdout stream (which is most likely the output you
> > see if you run the command at a terminal/console). *The second is what it printed to its
> > stderr stream which is a channel used for out of band data such as error or
> > status messages. *In this case, it is None, because you did open stderr as a
> > subprocess.PIPE.- Hide quoted text -

>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


stdout is a file object


All times are GMT. The time now is 08:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.