Stream not closed (ARCH2081)
Ce contenu n’est pas encore disponible dans votre langue.
Key python.performance.stream-not-closed · Severity error · Category performance
A stream opened with open but used without with is not closed on error or early return, leaking the file descriptor. Use a context manager.
Incorrect
Section titled “Incorrect”def read_config(path): f = open(path) data = f.read() return parse(data)Correct
Section titled “Correct”def read_config(path): with open(path) as f: data = f.read() return parse(data)Open streams with with open(...) as f: so they close automatically on block exit.