Pipes

os.popen() function

     f = popen("ls -l", "r")
     data = f.read()
     f.close()
  • Opens a pipe to or from a command and returns a file-object.

The popen2 module

  • Spawns processes and provides hooks to stdin, stdout, and stderr
     popen2(cmd)   # Run cmd and return (stdout, stdin)
     popen3(cmd)   # Run cmd and return (stdout, stdin, stderr) 
  • Example
     (o,i) = popen2.popen2("wc")
     i.write(data)         # Write to child's input
     i.close()             
     result = o.read()     # Get child's output
     o.close()
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 54
July 17, 2000, beazley@cs.uchicago.edu
>>>