# Open file and read contents.
with open('test.txt','r') as f:
= f.readlines()
output print(output)
['This file is called test.txt.\n', "This is what's on the second line."]
Mat Miller
December 13, 2022
This is a quick ‘today I learned’ (TIL) note on Python Context managers. Python context managers are used to wrap arbitrary code with entry (setup) and exit (cleanup) functions. One common places you’ll see them used is when reading data from a file.
['This file is called test.txt.\n', "This is what's on the second line."]
If we try and read from the file f
, defined above, we will get an I/O exception because the file as already been closed.
Here is the equivalent long hand way to read the data from the file:
['This file is called test.txt.\n', "This is what's on the second line."]
As you can see the syntax is more verbose, it would be easier to forget to close the file, and it’s much less clear to see at a glance when we’re operating on the file. This example is relatively trivial as we’re just reading all the lines of the text file into a list but you can probably imagine this could be a lot more complex if you were doing something more complicated like training a neural net.
Now let’s write our own class that uses a conext manager to cement how they can be implemented.
Entering the context...
hi My enter message.
Leaving the context...
None
None
None
As you can see the enter message was printed, the __enter__
return value was passed and then the exit message was printed. Now let’s see what happens if there is an error while within our context.
Entering the context...
My enter message.
Leaving the context...
<class 'ZeroDivisionError'>
division by zero
<traceback object>
ZeroDivisionError: division by zero
As you can see an error was thrown but the __exit__
function was run anyways.
There are many other ways you can implement and use context managers which you can read about here: Python Conext Managers. Hopefully I’ve given you a taste of what’s possible and given you a basic understanding of they they’re useful.
Here are a few more examples for your reference:
Example 1: Using the contextmanager
decorator
Example 2: Using ContextDecorator