hello I am pretty new into python , but I am trying to handle errors, however when I try to ctrl + C or L
I got input > ^Cerror
which it should be only display error
and not ^C or ^L
how can I fix this ?
except KeyboardInterrupt:
print("error")
except Exception as e:
raise e
You can back up and overwrite it, but you can't "fix" it. That character comes from the keyboard echo of the symbiont terminal: your stdin
processor is set to echo (print to screen) whatever is typed. That ^C
couplet is printed by this I/O processing before your program gets to handle the exception. Thus, the \\r
option (or a sequence of backspaces, \\b
) is likely your best remedy.
import sys
try:
while True:
string = input("Show me something")
except KeyboardInterrupt:
print("\b\b error")
except Exception as e:
raise e
Output, with CTRL-C at keyboard:
Show me something error
If you want to wipe out the entire input line, use \\r
instead:
except KeyboardInterrupt:
print("\rerror", " "*80)
Output:
Show me something -- this line appears until I hit CTRL-C. Replaced by
error
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.