Best way to capture output of another command-line program in Win32?

  • Thread starter Peter A. Schott
  • Start date
P

Peter A. Schott

Not sure what I should do here. I know that DOS/CMD can capture the output of
EXE files in Win32. I know that os.popen() has been recommended for this, but
the couple of times I've tried, it didn't seem to work well. I haven't had a
lot of need to do this recently, but we're trying to automate some processes
right now that require us to trap the output of a command line and react
accordingly.

Any suggestions on the best method for trapping this output?

Thanks.

-Pete
 
F

Fredrik Lundh

Peter said:
Not sure what I should do here. I know that DOS/CMD can capture the output of
EXE files in Win32. I know that os.popen() has been recommended for this, but
the couple of times I've tried, it didn't seem to work well. I haven't had a
lot of need to do this recently, but we're trying to automate some processes
right now that require us to trap the output of a command line and react
accordingly.

Any suggestions on the best method for trapping this output?

import subprocess

(see the library reference for details. if that doesn't work well, please post
a description of the problem, including tracebacks, test code, etc.)

</F>
 
G

Grig Gheorghiu

subprocess gets my vote too.

You can do something like:

from subprocess import call, Popen, PIPE, STDOUT

def run_cmd(cmd):
.....arglist = cmd.split()
.....p = Popen(arglist, stdout=PIPE, stderr=STDOUT)
.....output = p.communicate()[0]
.....return (p.returncode, output)

rc, output = run_cmd("python setup.py test")
if rc:
.....print "Command failed"
.....sys.exit(rc)

Grig
 
P

Peter A. Schott

Thanks, I'll give this a try when I get a chance and will post back if we have
any issues.

-Pete
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top