← How-To Guides How-To How to use a context manager Use with expr as x:. Create one with @contextmanager or a class with __enter__ / __exit__. with Ensures cleanup (e.g. close file). with open('f.txt') as f: content = f.read()