Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing string from python programs to external programs

Reply
Thread Tools

Passing string from python programs to external programs

 
 
lone_eagle
Guest
Posts: n/a
 
      05-26-2009
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
 
Reply With Quote
 
 
 
 
CTO
Guest
Posts: n/a
 
      05-26-2009
On May 26, 2:12*pm, lone_eagle <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Jeff McNeil
Guest
Posts: n/a
 
      05-26-2009
On May 26, 2:12*pm, lone_eagle <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
psykeedelik
Guest
Posts: n/a
 
      05-26-2009
On May 26, 9:16*pm, Jeff McNeil <(E-Mail Removed)> wrote:
> On May 26, 2:12*pm, lone_eagle <(E-Mail Removed)> wrote:
>
> > 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 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!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM
Running External Programs through ASP.NET =?Utf-8?B?TWF0dA==?= ASP .Net 2 05-13-2005 01:08 PM
Running External Programs from Within Python Bob=Moore Python 10 12-01-2004 12:37 AM
How do you execute external programs from Python? Carl Python 4 11-27-2004 07:47 PM
unresolved external symbol/using an external dll Scott Allen C++ 8 05-02-2004 06:11 PM



Advertisments