Siddharth Dushantha
Siddharth Dushantha

Reputation: 1491

How do I prevent ^C from showing when displayed when pressing CTRL+C

I am trying to prevent ^C from showing when a user presses CTRL+C while my script is running.

Why do I want to prevent that?

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

Answers (2)

Siddharth Dushantha
Siddharth Dushantha

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

mackorone
mackorone

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

Related Questions