A context manager is an object that is notified when a context (a block of code) starts and ends. You commonly use one with the with
statement. It takes care of the notifying.
For example, file objects are context managers. When a context ends, the file object is closed automatically:
open_file = open(filename)
with open_file:
file_contents = open_file.read()
# the open_file object has automatically been closed.
The above example is usually simplified by using the as
keyword:
with open(filename) as open_file:
file_contents = open_file.read()
# the open_file object has automatically been closed.
Anything that ends execution of the block causes the context manager's exit method to be called. This includes exceptions, and can be useful when an error causes you to prematurely exit from an open file or connection. Exiting a script without properly closing files/connections is a bad idea, that may cause data loss or other problems. By using a context manager you can ensure that precautions are always taken to prevent damage or loss in this way. This feature was added in Python 2.5.