Reading stdout and stderr of an external program

Discussion in 'Python' started by Murali, Jul 2, 2007.

  1. Murali

    Murali Guest

    Hi Python programmers,

    I need to be able to read the stdout and stderr streams of an external
    program that I launch from my python script. os.system( 'my_prog' +
    '>& err.log' ) and was planning on monitoring err.log and to display
    its contents. Is this the best way to do this?

    Thanks,
    Murali.
     
    Murali, Jul 2, 2007
    #1
    1. Advertising

  2. > I need to be able to read the stdout and stderr streams of an external
    > program that I launch from my python script. os.system( 'my_prog' +
    > '>& err.log' ) and was planning on monitoring err.log and to display
    > its contents. Is this the best way to do this?


    from subprocess import Popen
    stdout, stderr = Popen('my_prog').communicate()

    --Ben
     
    Ben Cartwright, Jul 2, 2007
    #2
    1. Advertising

  3. On Monday 02 July 2007, Murali wrote:
    > Hi Python programmers,
    >
    > I need to be able to read the stdout and stderr streams of an external
    > program that I launch from my python script. os.system( 'my_prog' +
    > '>& err.log' ) and was planning on monitoring err.log and to display
    > its contents. Is this the best way to do this?


    No. The best way to do this is with Popen, as it is portable and avoids the
    extranous file.


    --
    Regards, Thomas Jollans
    GPG key: 0xF421434B may be found on various keyservers, eg pgp.mit.edu
    Hacker key <http://hackerkey.com/>:
    v4sw6+8Yhw4/5ln3pr5Ock2ma2u7Lw2Nl7Di2e2t3/4TMb6HOPTen5/6g5OPa1XsMr9p-7/-6
     
    Thomas Jollans, Jul 2, 2007
    #3
    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. ladygrinningsoul

    Reading stdout and stderr separately

    ladygrinningsoul, Nov 18, 2004, in forum: Perl
    Replies:
    3
    Views:
    1,054
    Alexey A. Kiritchun
    Apr 25, 2005
  2. Christoph Haas
    Replies:
    0
    Views:
    397
    Christoph Haas
    Jun 13, 2006
  3. ChokSheak Lau

    C external execution - trap stdout + stderr

    ChokSheak Lau, Sep 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    289
    CBFalconer
    Sep 16, 2004
  4. Alexey Verkhovsky
    Replies:
    5
    Views:
    142
    Ara.T.Howard
    Jul 22, 2004
  5. Matt Mencel
    Replies:
    1
    Views:
    179
    ara.t.howard
    Jul 7, 2008
Loading...

Share This Page