Checking for EOF in stream

Grant Edwards grante at visi.com
Mon Feb 19 19:33:21 EST 2007


On 2007-02-19, GiBo <gibo at gentlemail.com> wrote:
> Hi!
>
> Classic situation - I have to process an input stream of unknown length
> until a I reach its end (EOF, End Of File). How do I check for EOF? The
> input stream can be anything from opened file through sys.stdin to a
> network socket. And it's binary and potentially huge (gigabytes), thus
> "for line in stream.readlines()" isn't really a way to go.
>
> For now I have roughly:
>
> stream = sys.stdin
> while True:
> 	data = stream.read(1024)
        if len(data) == 0:
             break  #EOF
> 	process_data(data)

-- 
Grant Edwards                   grante             Yow!  CALIFORNIA is where
                                  at               people from IOWA or NEW
                               visi.com            YORK go to subscribe to
                                                   CABLE TELEVISION!!



More information about the Python-list mailing list