obtain the output of an external program

Edvard Majakari edvard+news at majakari.net
Mon Nov 17 08:06:55 EST 2003


Fernando Rodriguez <frr at easyjob.net> writes:

> How can I save the output of an external program, called with os.system()?

Are you sure you need to use os.system? 


Python Library Documentation: module commands

NAME
    commands - Execute shell commands via os.popen() and return status, output.

FILE
    /usr/lib/python2.2/commands.py

DESCRIPTION
    Interface summary:
    
           import commands
    
           outtext = commands.getoutput(cmd)
           (exitstatus, outtext) = commands.getstatusoutput(cmd)
           outtext = commands.getstatus(file)  # returns output of "ls -ld file"
    
    A trailing newline is removed from the output string.
    
    Encapsulates the basic operation:
    
          pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
          text = pipe.read()
          sts = pipe.close()
    
     [Note:  it would be nice to add functions to interpret the exit status.]


-- 
# Edvard Majakari		Software Engineer
# PGP PUBLIC KEY available    	Soli Deo Gloria!
One day, when he was naughty, Mr Bunnsy looked over the hedge into Farmer
Fred's field and it was full of fresh green lettuces. Mr Bunnsy, however, was 
not full of lettuces. This did not seem fair.  --Mr Bunnsy has an adventure 




More information about the Python-list mailing list