In the previous post we went through a possible improvement of our first unreliable file I/O code using try-except-finally. The code is fine, it does what it is supposed to do. However, we’re still required to actively call the close function on the file handle. We’re dealing with resource management ourselves whereas we can ask Python to take care of it for us. If we forget to include the close method in our code then the file handle will stay open leading to all sorts of problems.

This is where context managers enter the scene. It’s a scary name that might at first imply a lot of complex code. We’ll in fact see that it makes our code shorter and more readable.

Context managers

Context managers are used with objects that go through some kind of open and close – or release – phases. Files fit this criteria especially well. They are opened and then ideally closed. Context managers will take care of the releasing phase for us in the background by closing the resource if it’s not needed any more.

We need to learn a new keyword: ‘with’. A with-block has much the same function as ‘using’ in C# and ‘try with resources’ in Java. Let’s see it in action:

The with keyword is following by the code that opens the resource, in this case the open function

We then assign that resource to a variable in an as-clause

Finally we call various functions on that resource

Note that the call to the close function is gone, it’s not necessary any more. The open method returns a file handler which is actually an implementation of a context manager that has a close method. The with keyword makes sure that Python will handling the resource releasing step behind the scenes.

You can use the with keyword with any resource that needs releasing when it’s not needed any more. You’ll probably use it most often in conjunction with files.

In the next post we’ll look at reading from a file. We’ll also see that we can have multiple with-blocks to handle multiple resources.