Skip to content

open-file-with-context-handler (SIM115)#

Derived from the flake8-simplify linter.

What it does#

Checks for uses of the builtin open() function without an associated context manager.

Why is this bad?#

If a file is opened without a context manager, it is not guaranteed that the file will be closed (e.g., if an exception is raised), which can cause resource leaks.

Example#

file = open("foo.txt")
...
file.close()

Use instead:

with open("foo.txt") as file:
    ...

References#