Skip to main content

How to Stop a Python Program Cleanly on Ctrl+C or Ctrl+D

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A program that reads input in a loop runs until someone stops it. Ctrl+C and Ctrl+D both stop it, but by default they print a traceback that looks like a crash. Catching the two exceptions they raise lets the program say goodbye instead. This guide shows the difference, with real output from Python 3.15.2.

Syntax​

Syntax
try:
<loop that reads input>
except KeyboardInterrupt, EOFError:
<clean-up and goodbye>
PartWhat it means
KeyboardInterruptRaised when the user presses Ctrl+C
EOFErrorRaised by input() at end of input: Ctrl+D on macOS and Linux, Ctrl+Z then Enter on Windows
except A, B:Catches either one. Since Python 3.14 the parentheses are optional; except (A, B): works in every version

Python's exceptions documentation: KeyboardInterrupt is "Raised when the user hits the interrupt key (normally Control-C or Delete)", and it "inherits from BaseException so as to not be accidentally caught by code that catches Exception". EOFError is "Raised when the input() function hits an end-of-file condition (EOF) without reading any data."

Prerequisites​

  • uv and Python 3.14 or newer for the form without parentheses. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: See What Happens Without It​

no_handling.py
while True:
line = input("> ")
print(f"You typed {line}")

Run it, type hi, then press Ctrl+C:

Terminal
uv run python no_handling.py
Output
> hi
You typed hi
> ^CTraceback (most recent call last):
File "/Users/you/source/python-basics/no_handling.py", line 2, in <module>
line = input("> ")
KeyboardInterrupt

It works, but the traceback looks like a crash.

Step 2: Catch Both Keys​

stop_cleanly.py
try:
while True:
line = input("> ")
print(f"You typed {line}")
except KeyboardInterrupt, EOFError:
print()
print("Bye")

The stop_cleanly.py script in a macOS-style code window: the input loop sits in a try block, and except KeyboardInterrupt, EOFError prints an empty line and Bye.

The empty print() moves to a new line, because the keypress leaves the cursor after the prompt.

Step 3: Run It Twice​

Run it, type hi, and press Ctrl+C. Then run it again and press Ctrl+D instead:

Terminal
uv run python stop_cleanly.py
uv run python stop_cleanly.py
Output
> hi
You typed hi
> ^C
Bye
> hi
You typed hi
> ^D
Bye

The output in a terminal: two runs, each echoing hi, then stopped once with Ctrl+C shown as ^C and once with Ctrl+D shown as ^D, each ending with Bye.

Both keys ended the loop the same way: Bye, no traceback.

Common Mistakes​

MistakeWhat happensDo this instead
Catching ExceptionCtrl+C still prints a traceback ending in KeyboardInterrupt, because it isn't an ExceptionName KeyboardInterrupt
Catching only KeyboardInterruptCtrl+D raises EOFError and prints a tracebackCatch both
Writing except KeyboardInterrupt, EOFError as error:SyntaxError: multiple exception types must be parenthesized when using 'as'Use except (KeyboardInterrupt, EOFError) as error:

Troubleshooting​

SymptomLikely causeFix
SyntaxError: multiple exception types must be parenthesizedPython older than 3.14 (this one came from Python 3.13)Write except (KeyboardInterrupt, EOFError):

FAQ​

Why does ^C appear?​

The terminal shows the key you pressed. The program's own output starts after it.

Should I catch Ctrl+C everywhere?​

Only around the part that waits for the user. Elsewhere, a traceback on Ctrl+C helps you see where the program was.

Conclusion​

Without handling, Ctrl+C printed a traceback. With except KeyboardInterrupt, EOFError: around the loop, Ctrl+C and Ctrl+D both ended the program with Bye.

Additional Resources​