calling an external program and capturing the output

E

Eric

Coming from a perl background I'm new to the Python world. I need to
read a list of values, send each value to an external program and
capture and act on the output of that program. Reading and parsing the
initial values is not a problem but I can't seem to find anything on
the sending to and capturing from an external program.

Any suggestions would be greatly appreciated.

-Eric
 
D

Diez B. Roggisch

Eric said:
Coming from a perl background I'm new to the Python world. I need to
read a list of values, send each value to an external program and
capture and act on the output of that program. Reading and parsing the
initial values is not a problem but I can't seem to find anything on
the sending to and capturing from an external program.

Any suggestions would be greatly appreciated.

See the module subprocess.

Diez
 
X

Xah Lee

Coming from a perl background I'm new to the Python world. I need to
read a list of values, send each value to an external program and
capture and act on the output of that program. Reading and parsing the
initial values is not a problem but I can't seem to find anything on
the sending to and capturing from an external program.

Any suggestions would be greatly appreciated.

See:

• Making System Calls in Perl and Python
http://xahlee.org/perl-python/system_calls.html

Xah
∑ http://xahlee.org/

☄
 
E

Eric

Thanks guys. That helped point me int he right direction.

with your advice on the subprocess module I stumbled upon this
posting:
http://www.velocityreviews.com/forums/t359866-subprocess-module.html

for anyone else that might be interested here is the solution. It
simply calls a perl script called add.pl that reads 2 numbers from
stdin and adds them together.

Thanks again for the help.

-Eric


#!/usr/bin/env python

import subprocess

prog = "./add.pl"
args = "3 4"

app = subprocess.Popen
(prog ,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)

print "opened " + prog
#print app.stdout.read()

print "writing \'" + args + "\' to " + prog + " subprocess"
app.stdin.write(args)
app.stdin.write("\n")

print "wrote \'" + args + "\' to " + prog + " subprocess"
result = app.stdout.read()
result = result.rstrip('\n')

print "received: " + result



and here is the output:

../call_add.py
opened ./add.pl
writing '3 4' to ./add.pl subprocess
wrote '3 4' to ./add.pl subprocess
received: 7
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top