<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Robert Rawlins - Think Blue wrote:
<blockquote
 cite="mid:000601c7870f$3b0a52e0$b11ef8a0$@rawlins@thinkbluemedia.co.uk"
 type="cite">
  <pre wrap="">Hello guys,

 

I've recently ported my application from bash to python, however there are
still a few bash line utilities I -have- to use in the application as there
isn't any alternative available to me. In the old days of bash I would have
grep'd the output from these commands to determine the outcome.

 

I'm now using popen2 to run the command which works a charm, but I'm
struggling to parse the results of the function, has anyone had any
experience with this? I've found a few suggested solutions dotted around,
such as this one.

 

     import os

 

     def filtered(command, source):

         dest, result = os.popen2(command)

         dest.write(source)

         dest.close()

         try:

              return result.read()

         finally:

              result.close()

 

But to be honest I'm struggling to get it to do anything as it doesn't
states what the 'source' object is or should be.

 

Thanks for any help guys, I'm just looking to capture the output from the
command and then I can go about a little REGEX on it.

 

Thanks,

 

Rob


  </pre>
</blockquote>
check out os.popen3 as well. An Example? Let's assume you were doing
'ls -1', which at the <br>
console would give you a one column list of files (and yes, I know I
can use glob.glob for this; <br>
this is an example).<br>
<blockquote>from os import popen3<br>
( sin, sout, serr ) = popen3( 'ls -1 /tmp' )<br>
fList = sout.readlines()<br>
for f in fList:<br>
    print f<br>
errors = serr.readlines()<br>
for line in errors:<br>
    print line<br>
</blockquote>
<br>
Now you have 3 file handles.<br>
sin: input (which I've never used, as I give popen3 a complete command)<br>
sout: the standard output stream<br>
serr: the standard error output<br>
You can read sout and serr and take action on them.<br>
Oh and each line has a '\n' linefeed (probably '\r\n' on Windows).
Depending on what your doing<br>
with the output, you might want to use string.strip to eliminate the
linefeeds.<br>
<br>
sph<br>
</body>
</html>