How to detect EOF from a stream ?

Troels Therkelsen t_therkelsen at hotmail.com
Tue Aug 27 14:28:37 EDT 2002


> " 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








More information about the Python-list mailing list