Can't detect EOF from stdin on windows console

Dmitry Anikin anikin#remove_this# at vstu.ru
Mon Apr 3 04:32:04 EDT 2006


I want to read stdin in chunks of fixed size until EOF
I want to be able (also) to supply data interactively in console
window and then to hit Ctrl+Z when finished
So what I do is:

while True:
    s = sys.stdin.read(chunk_size)
    if not s:
        break
# do something with s

if stdin is standard console input (on windows xp), here what happens:
(suppose, chunk_size = 3)
input: 123^Z<enter>
--- s gets "123" but reading doesn't end
input: ^Z<enter>
--- now s is empty and loop breaks,
so you have to press Ctrl-Z <Enter> TWICE to end the loop
worse still:
input: 12^Z<enter>
--- EOF is there, but there's only TWO chars instead of requested THREE,
so stdin.read() doesn't even return yet
input: ^Z<enter>
--- s gets "12" but reading doesn't end
input: ^Z<enter>
--- only now loop breaks
so you have to press Ctrl-Z <Enter> THRICE to end the loop

I haven't discovered any EOF function in python which could tell me
if eof was encountered. As you see, testing for empty string or for
len(s) = chunk_size doesn't improve the situation, anyone can
suggest a workaround?

Also I think the latter case is a straightaway bug, doc says:
      read( [size]) 

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). 
According to that, stdin.read(3), when supplied with "12^Z" should return immediately
with two-character string instead of waiting for third character after EOF.
By the way, if I enter something between ^Z's that will be treated as vaild input
and ^Z's will be completely ignored.



More information about the Python-list mailing list