G
GiBo
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)
process_data(data)
if len(data) < 1024: ## (*)
break
I smell a fragile point at (*) because as far as I know e.g. network
sockets streams may return less data than requested even when the socket
is still open.
I'd better like something like:
while not stream.eof():
...
but there is not eof() method :-(
This is probably a trivial problem but I haven't found a decent solution.
Any hints?
Thanks!
GiBo
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)
process_data(data)
if len(data) < 1024: ## (*)
break
I smell a fragile point at (*) because as far as I know e.g. network
sockets streams may return less data than requested even when the socket
is still open.
I'd better like something like:
while not stream.eof():
...
but there is not eof() method :-(
This is probably a trivial problem but I haven't found a decent solution.
Any hints?
Thanks!
GiBo