Hey, thanks for the nice explanation Michael!
Bernard On 6/9/06, Michael P. Reilly <[EMAIL PROTECTED]> wrote: > Excuse the slightly pedantic discussion before I get to a real-world > example, but the differences get into something a bit deeper than Python and > into what is called "interprocess communication". Each program that's > running has an input data stream and an output data steam and on many modern > computer systems there is a third stream for error messages. The idea is > that the program doesn't need to know that the input is coming from a user > or a keyboard; it could be coming from a file or another program - the > program shouldn't care. The same thing with the output and the error > messages. > > These three data streams have traditionally been called "stdin" (standard > input), "stdout" (standard output) and "stderr" (standard error). And in > Python, they are known by the same names in the 'sys' module. > > Now before I said that the concept of these is that the program should not > care where its data is going to or coming from, including another program. > This is where interprocess communication comes into play. Specifically, > this is accomplished with a data construct called "pipes" (the name 'popen' > comes from "pipe open" in the C world, akin to 'fopen' for "file open"). > Pipes are created between the programs to redirect data between inputs and > outputs. The most well known and viewed form of this is the vertical bar > "|" on command-lines in UNIX, then DOS had to adopt it: type README.txt | > more. > > Back to the question at hand: why and how would you use the files returned. > Let's say you have a large dictionary of values in your Python program, and > the system has a very nice spelling program out there. You would like to > use that program so you don't have to write you own routine. How the > spelling program is normally called is that words are sent to its standard > input (stdin) stream, separated by newlines, and the misspelled words are > printed on its stdout, also separated by newlines. We want to start the > program (called "spawning"), write our dictionary values to its stdin and > read its stdout. For this, we need to gain access to the program's stdin > and stdout. Since we are spawning it, it is called our "child" process. > > def verify_dictionary(dict_o_words): > reverse_dict = {} # to get back to the keys > (child_stdin, child_stdout) = os.popen2('spell') > for (key, value) in dict_o_words.items(): > if reverse_dict.has_key(value): > reverse_dict[value].append(key) > else: > reverse_dict[value] = [key] > child_stdin.write('%s\n' % value) > child_stdin.close() # close the data stream to tell other program we're > finished > misspellings = [] > for line in child_stdout.readlines(): > value = line.strip() > if reverse_dict.has_key(value): > misspellings.extend(reverse_dict[value]) > close_stdout.close() # tells other program we're finished reading from > it > return misspellings > > I hope this discussion answers your questions and hasn't been too beneath > you. :) Some of it might be arcane history now, but I always feel it is > good to know the roots to understand where you are going. > -Michael > > > On 6/9/06, Bernard Lebel <[EMAIL PROTECTED]> 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? > > Sorry the difference is very not clear to me. > > > Thanks > Bernard > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > > -- > 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