user1159495
user1159495

Reputation: 19

Read values from a running program

In a DOS .exe program (with no access to source code), the DOS screen says: rate = 60. (or some other value).

How do I read the output "60" to use as input to an application I'm making in Python or C/C++, other than by reading and typing it (I want to run it dozens of times).

This program runs in Windows XP mode.

Upvotes: 1

Views: 431

Answers (5)

Martin Brandl
Martin Brandl

Reputation: 59001

You can also invoke the application from your app and store the return value using ShellExecute.

Upvotes: 0

RanRag
RanRag

Reputation: 49587

Try Subprocess module to run dos.exe program from python code.

import shlex
cmdline = "command to execute your exe file"
cmd = shlex.split(cmdline)
output_process = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE).communicate()

Upvotes: 1

senderle
senderle

Reputation: 151137

Use the subprocess module. There are a few different ways; the most general is to use a Popen object.

>>> import subprocess
>>> proc = subprocess.Popen(['echo', 'rate = 60'], stdout=subprocess.PIPE,
                                                   stdin=subprocess.PIPE, 
                                                   stderr=subprocess.PIPE)
>>> proc.communicate()
('rate = 60\n', '')

If you're running Python 2.7 or higher, then you could also use subprocess.check_output.

Upvotes: 5

Sergey
Sergey

Reputation: 21241

In python:

import commands
commands.getoutput(cmd)

http://docs.python.org/library/commands.html#commands.getoutput

Upvotes: 2

asaelr
asaelr

Reputation: 5456

You can use pipe. prgm.exe | anotherprogram.exe Just remember that the anotherprogram will get the whole output of prgm.exe

Upvotes: 0

Related Questions