suppress — ignores specific exceptions cleanly.
1from contextlib import suppress23# Without suppress4import os5try:6 os.remove("file.txt")7except FileNotFoundError:8 pass910# With suppress11with suppress(FileNotFoundError):12 os.remove("file.txt")1314# Multiple exceptions15with suppress(FileNotFoundError, PermissionError, OSError):16 os.remove("file.txt")1718# In try/except patterns19with suppress(KeyError):20 value = my_dict["missing_key"]
Best practices:
except: pass.