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

Introduction

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:

try:
    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.

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: