contextlib.suppress in Python
Level 4151 keystrokes
This uses suppress() to silently ignore specific exceptions.
Python code
# suppress is a context manager that ignores the given exceptions1from contextlib import suppress# os provides file operations such as remove2import os3 # Same as try/except FileNotFoundError: pass, in one line4with suppress(FileNotFoundError): # The file does not exist, so this raises FileNotFoundError5 os.remove("nonexistent_file.txt") # Skipped: the rest of the block is abandoned after the exception6 print("Removed")7 # The exception was swallowed, so execution continues here8print("Continuing...")Output
Continuing...
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
"× 6.× 5(× 4)× 4_× 1:× 1