Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Passing string from python programs to external programs (http://www.velocityreviews.com/forums/t685651-passing-string-from-python-programs-to-external-programs.html)

lone_eagle 05-26-2009 06:12 PM

Passing string from python programs to external programs
 
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

CTO 05-26-2009 07:09 PM

Re: Passing string from python programs to external programs
 
On May 26, 2:12*pm, lone_eagle <icym...@gmail.com> 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

Jeff McNeil 05-26-2009 07:16 PM

Re: Passing string from python programs to external programs
 
On May 26, 2:12*pm, lone_eagle <icym...@gmail.com> 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

psykeedelik 05-26-2009 09:16 PM

Re: Passing string from python programs to external programs
 
On May 26, 9:16*pm, Jeff McNeil <j...@jmcneil.net> wrote:
> On May 26, 2:12*pm, lone_eagle <icym...@gmail.com> 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!!


All times are GMT. The time now is 03:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57