Passing string from python programs to external programs

L

lone_eagle

Hi all,

On Linux, I do something like this

$ program_to_execute < input_file
.... get some output ...

I have the content of the input_file as a string inside a python
program and would like to pass this string to the external program
from inside the python program and get back the programs output in a
string/file. Can someone tell me how to achieve this. I have been
through the documentation for Popen, but this one beats me.

Cheers,
Chaitanya
 
C

CTO

Hi all,

On Linux, I do something like this

$ program_to_execute < input_file
... get some output ...

I have the content of the input_file as a string inside a python
program and would like to pass this string to the external program
from inside the python program and get back the programs output in a
string/file. Can someone tell me how to achieve this. I have been
through the documentation for Popen, but this one beats me.

Cheers,
Chaitanya

from subprocess import getstatusoutput

cmd = 'echo '
str = 'Hello World!'
status, output = getstatusoutput(cmd + repr(str))

Obviously, this is 3.x. I believe that in 2.x it was in
the commands module.

Geremy Condra
 
J

Jeff McNeil

Hi all,

On Linux, I do something like this

$ program_to_execute < input_file
... get some output ...

I have the content of the input_file as a string inside a python
program and would like to pass this string to the external program
from inside the python program and get back the programs output in a
string/file. Can someone tell me how to achieve this. I have been
through the documentation for Popen, but this one beats me.

Cheers,
Chaitanya

Sounds like subprocess is what you want. Here's a quick ad-hoc
example. You can find more information on the module at
http://docs.python.org/library/subprocess.html.

[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import subprocess
sub = subprocess.Popen('/bin/gzip', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
g = sub.communicate('Please, sir, gzip me?')
import gzip
import StringIO
gzip.GzipFile(fileobj=StringIO.StringIO(g[0])).read() 'Please, sir, gzip me?'

Thanks,

Jeff
mcjeff.blogspot.com
 
P

psykeedelik

On Linux, I do something like this
$ program_to_execute < input_file
... get some output ...
I have the content of the input_file as a string inside a python
program and would like to pass this string to the external program
from inside the python program and get back the programs output in a
string/file. Can someone tell me how to achieve this. I have been
through the documentation for Popen, but this one beats me.
Cheers,
Chaitanya

Sounds like subprocess is what you want.  Here's a quick ad-hoc
example. You can find more information on the module athttp://docs.python..org/library/subprocess.html.

[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> import subprocess
sub = subprocess.Popen('/bin/gzip', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
g = sub.communicate('Please, sir, gzip me?')
import gzip
import StringIO
gzip.GzipFile(fileobj=StringIO.StringIO(g[0])).read()

'Please, sir, gzip me?'



Thanks,

Jeff
mcjeff.blogspot.com

Thanks guys!! Problem solved!!
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top