I have read, that file opened like this is closed automatically when leaving the with block:
with open("x.txt") as f:
data = f.read()
do something with data
yet when opening from web, I need this:
from contextlib import closing
from urllib.request import urlopen
with closing(urlopen('http://www.python.org')) as page:
for line in page:
print(line)
why and what is the difference? (I am using Python3)
The details get a little technical, so let's start with the simple version:
Some types know how to be used in a with
statement. File objects, like what you get back from open
, are an example of such a type. As it turns out, the objects that you get back from urllib.request.urlopen
, are also an example of such a type, so your second example could be written the same way as the first.
But some types don't know how to be used in a with
statement. The closing
function is designed to wrap such types—as long as they have a close
method, it will call their close
method when you exit the with
statement.
Of course some types don't know how to be used in a with
statement, and also can't be used with closing
because their cleanup method isn't named close
(or because cleaning them up is more complicated than just closing them). In that case, you need to write a custom context manager. But even that isn't usually that hard.
In technical terms:
A with
statement requires a context manager, an object with __enter__
and __exit__
methods. It will call the __enter__
method, and give you the value returned by that method in the as
clause, and it will then call the __exit__
method at the end of the with
statement.
File objects inherit from io.IOBase
, which is a context manager whose __enter__
method returns itself, and whose __exit__
calls self.close()
.
The object returned by urlopen
is (assuming an http
or https
URL) an HTTPResponse
, which, as the docs say, "can be used with a with
statement".
The closing
function:
Return a context manager that closes thing upon completion of the block. This is basically equivalent to:
@contextmanager
def closing(thing):
try:
yield thing
finally:
thing.close()
It's not always 100% clear in the docs which types are context managers and which types aren't. Especially since there's been a major drive since 3.1 to make everything that could be a context manager into one (and, for that matter, to make everything that's mostly-file-like into an actual IOBase
if it makes sense), but it's still not 100% complete as of 3.4.
You can always just try it and see. If you get an AttributeError: __exit__
, then the object isn't usable as a context manager. If you think it should be, file a bug suggesting the change. If you don't get that error, but the docs don't mention that it's legal, file a bug suggesting the docs be updated.