How to Stop a Python Program Cleanly on Ctrl+C or Ctrl+D
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
try:
<loop that reads input>
except KeyboardInterrupt, EOFError:
<clean-up and goodbye>
| Part | What it means |
|---|---|
KeyboardInterrupt | Raised when the user presses Ctrl+C |
EOFError | Raised 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
while True:
line = input("> ")
print(f"You typed {line}")
Run it, type hi, then press Ctrl+C:
uv run python no_handling.py
> 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
try:
while True:
line = input("> ")
print(f"You typed {line}")
except KeyboardInterrupt, EOFError:
print()
print("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:
uv run python stop_cleanly.py
uv run python stop_cleanly.py
> hi
You typed hi
> ^C
Bye
> hi
You typed hi
> ^D
Bye

Both keys ended the loop the same way: Bye, no traceback.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Catching Exception | Ctrl+C still prints a traceback ending in KeyboardInterrupt, because it isn't an Exception | Name KeyboardInterrupt |
Catching only KeyboardInterrupt | Ctrl+D raises EOFError and prints a traceback | Catch both |
Writing except KeyboardInterrupt, EOFError as error: | SyntaxError: multiple exception types must be parenthesized when using 'as' | Use except (KeyboardInterrupt, EOFError) as error: |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
SyntaxError: multiple exception types must be parenthesized | Python 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
- KeyboardInterrupt and EOFError — the two exceptions.
- Using the Python interpreter — the end-of-file keys on Unix and Windows.
