How to detect EOF from a stream ?

Spendius spendius at muchomail.com
Mon Aug 26 16:48:22 CEST 2002


Hi,
I'm a newbie in Python (and found no answer when searching to the
question I'm asking here): if with the following Java 
" import java.io.*;
" class t12
" {
"   public static void main(String args[])
"   {
"     t12 test = new t12();
"   }
" 
"   public t12()
"   {
"     Process osProcess = null;
"     try {
"       osProcess = Runtime.getRuntime().exec("<cmd>");
" 
"       InputStream is = new BufferedInputStream(osProcess.getInputStream());
" 
"       int numChar = 0;
"       while (numChar != -1)
"       {
"         numChar = is.read();
"         System.out.println(numChar+" "+(char)numChar);
"       }
"     } catch (IOException ioe) {
"     }
"   }
" }

I can see the EOF with the value -1 assigned to the var. 'numChar',
with the following Python:

" import popen2, sys
" 
" fromP, toP = popen2.popen2('<cmd>')
" char = '#'
" while 1:
"     for char in fromP.read(1):
"         sys.stdout.softspace=0
"         charOrd = ord(char)
"         print char,  charOrd
" 
" print
" print 'OK sortie de boucle'
" 

I don't get the EOF because my program hangs. I'd like to be
able to address the EOF character, and get rid of the hanging
of my program: what should I do ? How should I write my 
condition on the 'while 1:' line ? And is it possible to
trap the end of my stream just like in Java with this '-1'
value ??

Thanks a lot !
Regards,
Sp



More information about the Python-list mailing list