newbie:trying to read in stdoutput of commands under various *nixes

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Tue Jul 13 18:14:06 EDT 2004


googleboy wrote:

> #!/usr/bin/python
> 
> import string, os
> 
> name = "uname"
> outName = os.popen ( name, "w" )
> print outName
> 
> =+=
> 
> and get output like:
> 
> <open file 'uname', mode 'w' at 0x8169f90>
> Linux

Here you are opening a pipe in "write" mode. uname writes to stdout
(hence the "Linux" line) then returns a file-like object that you can
write to. That's the first line.

What you want is to open the process in "read" mode, then read the data
from it. Untested:

import os
pin = os.popen("uname", "r")

try:
    print pin.read()
finally:
    pin.close()

Tim Delaney



More information about the Python-list mailing list