Re: [Tutor] Difference between popens
On 6/9/06, Bernard Lebel [EMAIL PROTECTED] wrote: Hey, thanks for the nice explanation Michael!BernardWhoops.. Hit reply instead of reply to all. My apologies to the group. Dang gmail. -Michael -- There's so many different worlds,So many different suns.And we have just one world,But we live in different ones. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Difference between popens
Hi, I'd like to know what are the differences at the various os.popenX flavors. I read the documentation and I can see they return file objects. so what can you do with these file objects? I mean, why would you need a set of file objects rather than another? Sorry the difference is very not clear to me. Thanks Bernard ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between popens
On Fri, Jun 09, 2006 at 03:38:58PM -0400, Bernard Lebel wrote: Hi, I'd like to know what are the differences at the various os.popenX flavors. I read the documentation and I can see they return file objects. so what can you do with these file objects? I mean, why would you need a set of file objects rather than another? See documentation at http://docs.python.org/lib/os-newstreams.html#l2h-1552. And, notice how the return values from the various versions of popen are different file types: stdin, stdout, and stderr. A summary: - popen() gives you either an input pipe (stdin) or an output pipe (stdout) but not both. - popen2() gives you both an input pipe (stdin) and output pipe (stdout). - popen3() gives you an input pipe (stdin) and output pipe (stdout) and an error pipe (stderr). - popen4() gives you an input pipe and a pipe that combines output and errors (stdout and stderr). Specifically, if you want to run a command, just like you would with os.system(), but: 1. You want to *either* feed (pipe) text to your command *or* read results from your command, use os.popen() and use mode= 'w' or 'r'. 2. You want to both feed (pipe) text to your command *and* read results from your command, use os.popen2(). Etc. If you get an input pipe, you should write text to that pipe, then close that stream. Doing close() is what triggers execution of the command. If you get an output pipe, then (after the command runs), read from that pipe to get the results of your command (i.e. the text that the command wrote to stdout). Here is a simple example that uses popen2:: import os def test(): instream, outstream = os.popen2('grep dave') instream.write('Line #1\n') instream.write('Line #2 dave is here\n') instream.write('Line #3\n') instream.write('Line #4 dave is also here\n') instream.write('Line #5\n') instream.close() for line in outstream: print 'Line: %s' % line.rstrip() test() Note that there is also a popen module, which has functions with the same names and functionality: This functionality is also available in the popen2 module using functions of the same names, but the return values of those functions have a different order. See: http://docs.python.org/lib/module-popen2.html Hope this helps. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between popens
I'd like to know what are the differences at the various os.popenX flavors. I read the documentation and I can see they return file objects. so what can you do with these file objects? I mean, why would you need a set of file objects rather than another? My OS topic covers some of the popen variants with explanation. It might help. Here are the most relevant two paragraphs: -- In fact there are several variations of the popen command called popen, popen2, popen3 and popen4. The numbers refer to the various data stream combinations that are made available. The standard data streams were described in a sidebar in the Talking to the User topic. The basic version of popen simply creates a single data stream where all input/output is sent/received depending on a mode parameter passed to the function. In essence it tries to make executing a command look like using a file object. By contrast, popen2 offers two streams, one for standard output and another for standard input, so we can send data to the process and read the output without closing the process. popen3 provides stderr access in addition to stdin/stdout. Finally there is popen4 that combines stderr and stdout into a single stream which appears very like normal console output. In Python 2.4 all of these popen calls have been superseded by a new Popen class found in a new subprocess module which we will look at later. For now we will only look at the standard os.popen() function, the others I will leave as a research exercise! --- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor