Python language basics 86: file I/O resource management in a context manager


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:

    fullpath = os.path.join('C:/', 'nosuchfolder', 'source.txt')
    with open(fullpath, mode='wt', encoding='UTF-8') as file:
        file.write('This is some content, ')
        file.write('this is some more content, ')
        file.write('good bye for now.')
except IOError as e:
    print('Error: {0}'.format(e))

The ‘with’ block consists of 3 parts:

  • 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.

Read all Python-related posts on this blog here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: