Passing string from python programs to external programs
icymist at gmail.com
Tue May 26 17:16:11 EDT 2009
On May 26, 9:16 pm, Jeff McNeil <j... at jmcneil.net> wrote:
> On May 26, 2:12 pm, lone_eagle <icym... at 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)).read()
> 'Please, sir, gzip me?'
Thanks guys!! Problem solved!!
More information about the Python-list