This pattern (set up, do work, clean up) is common across many different problem domains, so Python has a language feature to help with it. Context managers, defined using the with keyword, manage the setup and cleanup processes. Python's file objects support them, as do most database libraries.