How to detect EOF from a stream ?

Sebastien Bigaret Sebastien.Bigaret at inqual.com
Tue Aug 27 20:58:21 CEST 2002


And beware: if '<cmd>' is an other python program, you *must* close
file-descr. #1: 'os.close(1)' : closing sys.stdout is not sufficient and
you'll never get the EOF in that case (sys.stdout.close() does not close the
underlying file descriptor, opened at the beginning of the process)

(same for stderr/file desc. #2) 


> > " 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
> 
> >From the standard docs on the File Object's read() method
> (http://www.python.org/doc/current/lib/bltin-file-objects.html
> specifically):
> 
>    "An empty string is returned when EOF is encountered
>     immediately."
> 
> Regards,
> 
> Troels Therkelsen
> 
> 
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list