2. Deleting a file

If missing files are acceptable, contextlib.suppress keeps the intent clear.

Deleting a file is often best-effort cleanup, so treating a missing file as noise can be reasonable. The goal is to make that choice explicit without broadening the exception handling more than necessary.

2.1. Don’t do this

1import os
2
3try:
4    os.remove('test.tmp')
5except OSError:
6    pass

2.2. Do this

1import os
2from contextlib import suppress
3
4with suppress(OSError):
5    os.remove('test.tmp')