What ? The with statement is used to wrap the execution of a code block, in such a manner that a runtime context is set up, before the code block executes. the runtime context is teared down, after the code block has executed (no matter if an exception is raised inside of the code block). To do this, the with statement depends on a context manager , which handles the set up and tear down operations. Why ? The with statement is a generalization of the try-finally construct. From the python docs: If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception, it is re-raised at the end of the finally clause. If the finally clause raises another exception or executes a return or break statement, the saved exception is discarded: de...