Reputation: 1491
I am trying to prevent ^C
from showing when a user presses CTRL+C
while my script is running.
Because things like this will happen and it does not look nice:
$ python3 myscript.py
^CYour pressed CTRL+C
I know there is a similar question here, but it does not work in Python
Upvotes: 1
Views: 1101
Reputation: 1491
I found an easy method!
# Returning the cursor to home and dont create a new line
print("\r", end="")
# Now we are able to print on the line where ^C would be displayed
print("Your pressed CTRL+C")
Upvotes: 2
Reputation: 1066
Because things like this will happen...
Just my two cents: the fact that ^C
displays in the terminal is a good thing. It's confirmation that SIGINT
was sent to the process, as expected. Don't try to remove it; instead, as others have suggested, start a new line if you really want to. Or just exit without printing anything additional at all, like lots of other command line applications.
Upvotes: 1