get the output of a cmd via popen2

Bill Bell bill-bell at bill-bell.hamilton.on.ca
Sun Aug 26 09:36:25 EDT 2001


Rajarshi Guha <rajarshi at seed.chem.psu.edu> wrote, in part:
> I have a command which I want to run from a Python script and I want
> to capture the output. So I do,
> 
> i,o = os.popen2(cmd)
> 
> where cmd is the string that represents the command. But on printing o
> all I get is an object representation like: 
> 
> <open file '(fdopen)', mode 'r' at 0x810d9f8>

'i' and 'o' are like file objects. You need to write and read them 
respectively, like so:

>>> import os
>>> i, o = os . popen2 ( 'ver' ) # 'ver' returns OS version
>>> print o . read ( ) # read everything from 'o' and print it

Windows 95. [Version 4.00.1111]





More information about the Python-list mailing list