Content · Programming · Python

Python contextlib.suppress() Function: Usage and Examples

suppress(*exception_types) deliberately ignores only selected exception classes inside a with block.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

suppress(*exception_types) deliberately ignores only selected exception classes inside a with block.

What It Does and Why It Matters


suppress(*exception_types) deliberately ignores only selected exception classes inside a with block. Use it for expected, harmless failures such as deleting an optional file.

Target the narrowest expected exception to avoid concealing failures.

Correct cleanup means resources are released when exceptions occur, not only on the success path.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

from contextlib import suppress
from pathlib import Path
with suppress(FileNotFoundError):
    Path('nonexistent-optional-483927.tmp').unlink()
print('Cleanup completed')

How the Code Works


  • The imports make these standard-library tools available: contextlib, pathlib.
  • The structured block controls error handling or resource lifetime, including what happens when an operation fails.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.
  • The manager establishes a boundary for acquiring and releasing the resource; observe the order of setup, body execution and cleanup.

Expected output:

Cleanup completed

For the demonstrated inputs, Python displays 'Cleanup completed'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This matters when working with files, locks, sockets and connections whose lifetime must be controlled. A reliable implementation defines ownership and releases resources on both successful and exceptional paths. Use a small with block before building a more complex manager.

Practice Exercise


Ignore a missing optional path without swallowing unrelated errors.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Suppressing Exception broadly and hiding permission failures or program bugs.
  • Overlooking the documented condition: suppress(*exception_types) deliberately ignores only selected exception classes inside a with block.

Key Takeaways


  • suppress(*exception_types) deliberately ignores only selected exception classes inside a with block.
  • Ignore a missing optional path without swallowing unrelated errors.
  • Correct cleanup means resources are released when exceptions occur, not only on the success path.
Share this resourceSend this page to someone who may find it useful.