Passing string from python programs to external programs

Discussion in 'Python' started by lone_eagle, May 26, 2009.

  1. lone_eagle

    lone_eagle Guest

    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
     
    lone_eagle, May 26, 2009
    #1
    1. Advertising

  2. lone_eagle

    CTO Guest

    On May 26, 2:12 pm, lone_eagle <> 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
     
    CTO, May 26, 2009
    #2
    1. Advertising

  3. lone_eagle

    Jeff McNeil Guest

    On May 26, 2:12 pm, lone_eagle <> 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
     
    Jeff McNeil, May 26, 2009
    #3
  4. lone_eagle

    psykeedelik Guest

    On May 26, 9:16 pm, Jeff McNeil <> wrote:
    > On May 26, 2:12 pm, lone_eagle <> 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!!
     
    psykeedelik, May 26, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWF0dA==?=

    Running External Programs through ASP.NET

    =?Utf-8?B?TWF0dA==?=, May 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,321
    =?Utf-8?B?TWF0dA==?=
    May 13, 2005
  2. Scott Allen
    Replies:
    8
    Views:
    11,044
    Scott Allen
    May 2, 2004
  3. Bob=Moore
    Replies:
    10
    Views:
    2,819
  4. Carl
    Replies:
    4
    Views:
    5,315
    Paul Watson
    Nov 27, 2004
  5. Mellow Crow
    Replies:
    6
    Views:
    438
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page