473,804 Members | 3,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4502
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
10038
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 in IE? Can anyone help me? Thanx in advance,
3
300
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) // Process Mouse up in output TextBox {
2
19939
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 discovered it is holding Windows open when you shutdown. If the application is running and the user shutsdown Window it holds up everything. If the application is not running and the user shutsdown Windows is shutsdown in a timely manner. How...
12
17065
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 is being closed? It is easy enough if the user selects "exit" from this tray icon but how can you detect if Windows is closing the program down? Normally I would simply do something in the Form.Closing event but without a form how can you...
5
2184
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 packets.Please tell me any website refrences or any Source code examples.This is very urgent requirement. Thanks in advance, Regards, Tulasi Kumar.
0
1066
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
984
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
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.