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

N

noydb

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

Tim Harig

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

Tim Harig

^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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
 
J

Jean-Michel Pichavant

Tim said:
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
 
N

noydb

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

Tim Harig

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

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

Tim Harig

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

noydb

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
 
T

Tim Harig

On 2010-11-18, noydb <[email protected]> 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()
 
N

noydb

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.




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
 
N

noydb

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- Hide quoted text -

- Show quoted text -

if I do('', None)

Doesn't that mean it is empty?
 
N

noydb

Any other help? I am guessing not, just wanted to try one more time.
Could really use help, please!!
 
J

Jerry Hill

Any other help?  I am guessing not, just wanted to try one more time..
Could really use help, please!!

You'll need to give us more information about the program you're
trying to automate. It originally sounded like you just needed to run
a console program, where it's usually fairly easy to run and capture
the output with the subprocess module. Now it sounds like you're
trying to automate a GUI that requires user interaction. That's quite
a bit more complicated.

When you run "stats_hall.exe", what do you get on the screen? What,
exactly, are the steps a normal user would need to perform to do what
you want to automate? After the values you're interested in are
computed, where do they show up? Is this program publically available
someplace?

I've generally had good luck doing simple GUI automation with
pywinauto. The homepage appears to be: http://pywinauto.pbworks.com
which has installation instructions and a FAQ. You might also find
this demo useful:
http://showmedo.com/videotutorials/video?name=UsingpyWinAutoToControlAWindowsApplication
 
T

Tim Harig

C:\Documents and Settings\Tim Harig\My Documents\autoCalc>dir
Volume in drive C has no label.
Volume Serial Number is 30D9-35E0

Directory of C:\Documents and Settings\Tim Harig\My Documents\autoCalc

11/19/2010 12:20 PM <DIR> .
11/19/2010 12:20 PM <DIR> ..
11/19/2010 12:19 PM 686 autoCalc.pys
1 File(s) 686 bytes
2 Dir(s) 16,343,552,000 bytes free

C:\Documents and Settings\Tim Harig\My Documents\autoCalc>type autoCalc.pys
# autoCalc.pys: The "pys" extension indicates that it should be run under
# Windows Script Host

# perform the calculation using Windows calculator
keySequence = ['2', '{+}', '2', '=', '^c', '%{F4}']
WshShell = WScript.CreateObject("WScript.Shell")
calculator = WshShell.Run("calc")
WshShell.AppActivate("calc")
WScript.Sleep(1000)
for currentKey in keySequence:
WshShell.SendKeys(currentKey)
WScript.Sleep(100)

# write the results to notepad and same as demo.txt
keySequence = ['result: ', '^v', '^s',
'c:\\Documents and Settings\\Tim Harig\\My Documents\\autoCalc\\demo.txt',
'~', '%{F4}']
notepad = WshShell.Run("notepad")
WshShell.AppActivate("notepad")
WScript.Sleep(1000)
for currentKey in keySequence:
WshShell.SendKeys(currentKey)
WScript.Sleep(100)

C:\Documents and Settings\Tim Harig\My Documents\autoCalc>cscript.exe autoCalc.pys
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Debugging extensions (axdebug) module does not exist - debugging is disabled..

C:\Documents and Settings\Tim Harig\My Documents\autoCalc>type demo.txt
result: 4
C:\Documents and Settings\Tim Harig\My Documents\autoCalc>
 
N

noydb

Thanks to Jerry Hill above who helped.
This worked:

from pywinauto.application import Application
app = Application()
app.start_(r'C:\temp\hallbig2.exe')
app.Form1.Edit6.TypeKeys(r'C:\temp\input\Ea39j.txt')
E_Value = ""
while (E_Value == ""):
app.Form1.Compute.Click()
E_Value = app.Form1.Edit8.WindowText()
print repr(E_Value)
app.Kill_()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top