Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between all of the os.popen() methods?

I was looking at the Python documentation and saw that there are 4-5 different versions of popen(), e.g. os.popen(), os.popen2(), etc.

Apart from the fact that some include stderr while others don't, what are the differences between them and when would you use each one? The documentation didn't really explain it very well.

like image 514
crystalattice Avatar asked Nov 09 '08 08:11

crystalattice


3 Answers

Jason has it right. To summarize in a way that's easier to see:

  • os.popen() -> stdout
  • os.popen2() -> (stdin, stdout)
  • os.popen3() -> (stdin, stdout, stderr)
  • os.popen4() -> (stdin, stdout_and_stderr)
like image 163
Ned Batchelder Avatar answered Nov 13 '22 20:11

Ned Batchelder


I would recommend to use the subprocess module which has all the features that these functions have and more.

like image 43
J S Avatar answered Nov 13 '22 22:11

J S


popen2 doesn't capture standard error, popen3 does capture standard error and gives a unique file handle for it. Finally, popen4 captures standard error but includes it in the same file object as standard output.

like image 10
Jason Coco Avatar answered Nov 13 '22 21:11

Jason Coco