with
statement. open()
together with a with
statement like so: with open('foo.txt') as f: text = f.read()
with
block is exited, the context manager automatically calls the f.close()
. contextmanager
(in the standard library contextlib
) is a decorator that turns a function into a context manager.