[Tutor] EOF problems
Michael Janssen
Janssen at rz.uni-frankfurt.de
Thu Jan 29 09:04:10 EST 2004
On Wed, 28 Jan 2004, Christopher Spears wrote:
> My instructor claims that Ctrl-Z in Windows is the
> equivalent of an EOF. However, this is not what I
> have discovered. Ctrl-Z is the undo command in IDLE.
> Instead, I used Ctrl-C, which is the copy command.
> Since there was an empty string, the script stopped.
> Here is the result:
the gui or the application might catch every controlkey before it gets
passed to python and raw_input. Try to receive alt+F4 with raw_input and
you know what I mean ;-)
When idle catches crtl-Z, there's not much you can do about (unless
configure/rewrite idle). Once I have defined a shortcut strg-alt-Q
unders w98 and I needed a long time to realize why the heck notepad
didn't want and @ sign any longer (uhm, under windows - w98 at least
- strg+alt modifier acts the same like "Alt Gr").
Besides such wonders, your task wouldn't be solved once you found a EOF
key. What is python supposed to do, when an function like raw_input
recieved EOF instead of string-input? raw_input then can't return a
string and must therefore throw an exception to inform the user, there
is no string, not even the empty string.
Look here under linux where EOF is crtl-D:
>>> raw_input("I will type in ctrl+D soon: ")
I will type in ctrl+D soon: Traceback (most recent call last):
File "<stdin>", line 1, in ?
EOFError
This means, when you want to interrupt a while+raw_input loop with
non-string-input, you must catch the exception:
while 1:
try:
s = raw_input("prompt: ")
except (EOFError, KeyboardInterrupt):
# user pressed ctrl-D (under linux)
# or ctrl-C --> KeyboardInterrupt
print # end in newline
break
In case ctrl-C will allways trow TypeError (and isn't dependend of the
content of the clipboard), you found your solution and only have to
silence the Exception.
Michael
> >>>
> Type a string:asdfjk;l
> Type a string:asdfj;
> Type a string:asdfj
>
> Traceback (most recent call last):
> File "C:\Documents and Settings\Christstopher
> Spears\My Documents\python\unit7question2.py", line 4,
> in -toplevel-
> s = raw_input ('Type a string:')
> TypeError: object.readline() returned non-string
> >>>
>
> Ok, so the script works, but what is the EOF signal in
> IDLE? Reading the docs, I seemed to gather that when
> reading a file, Python sees an empty string as the
> EOF. Am I wrong? Is there some variable that can be
> used to signal EOF to Python? I have already tried
> eof.
>
> -Chris
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list