473,378 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Can't detect EOF from stdin on windows console

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.
Apr 3 '06 #1
2 4464
Dmitry Anikin wrote:
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
[snip fighting with windows]
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.


On windows EOF is recognized only in the beginning of a line. That is
how almost all console applications work on windows. So you have to use
either .readline() method or .read(1)

Apr 3 '06 #2
Serge Orlov wrote:
Dmitry Anikin wrote:
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


[snip fighting with windows]
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.


On windows EOF is recognized only in the beginning of a line. That is
how almost all console applications work on windows. So you have to use
either .readline() method or .read(1)


Weird... I never knew this, although I had noticed-in-passing that
hitting ^Z to exit the interpreter didn't always work as intended. Nice
to have an explanation why.

On a related note though: EOF _is_ recognized in mid-line if you are
just reading from a file (in text mode, obviously, not binary mode). I
wonder what freakish design decisions led to that situation...

-Peter

Apr 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Yurij Nykon | last post by:
Hi all. How can I detect the version of Flash-Plugin installed in Internet Explorer? In Netscape i can do this with following java script navigator.plugins.description But it doesn't works...
3
by: Harry J. Smith | last post by:
How can you detect a mouse double click on a text box? I tried the following but it does not work. private void richTextOut_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) //...
2
by: Mike Stephens | last post by:
I have an application to minimizes when X is clicked. If the user wants to close the application they click the Exit Application button.This works fine and does exactly what I need. I have since...
12
by: Patrick Dugan | last post by:
I have an vb.net application that is a module that uses a "application.run" in the sub main to start. There is no form involved (just a system tray icon) How can you detect when the application...
5
by: TulasiKumar | last post by:
Hi all, I am new in Netwrok porgraming in cSharp.how can i detect Tcp/Ip packet in c#.Net.Any body knows please tell me, what are the interfaces or classes are supported regrading on TCP/IP...
0
by: TheSteph | last post by:
Hi, In Windows Forms : How can I detect that a Shortcut (I mean the Shortcut enum) has been pressed ? Thanks,
0
by: Raheleh | last post by:
need to acccess to a Microsoft Word file and process it in windows Console Application. I can't save the file as Plain text and process it, because its format will change. Thanks in advance
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.