Should I Call Close() After Urllib.Urlopen()

should I call close() after urllib.urlopen()?

The close method must be called on the result of urllib.urlopen, not on the urllib module itself as you're thinking about (as you mention urllib.close -- which doesn't exist).

The best approach: instead of x = urllib.urlopen(u) etc, use:

import contextlib

with contextlib.closing(urllib.urlopen(u)) as x:
...use x at will here...

The with statement, and the closing context manager, will ensure proper closure even in presence of exceptions.

closing files properly opened with urllib2.urlopen()

Why not just try closing sf, and passing if it doesn't exist?

import urllib2
try:
search_query = 'http://blah'
sf = urllib2.urlopen(search_query)
search_soup = BeautifulSoup.BeautifulStoneSoup(sf.read())
except urllib2.URLError, err:
print(err.reason)
finally:
try:
sf.close()
except NameError:
pass

Is there an advantage to using contextlib.closing when making a request using urllib.request.urlopen?

contextlib.closing() is useful for turning objects that support a close() method into a context manager that can be used with a with statement.

It adds no value to objects that already support the context manager protocol.

The documentation for urllib.request.urlopen() explains:

For ftp, file, and data urls and requests explicitly handled by legacy URLopener and FancyURLopener classes, this function returns a urllib.response.addinfourl object which can work as context manager

So in this case, there is no point in wrapping it in a closing function.

Python - How to make sure resource is closed

Open the connection within a context manager using with as:

import urllib.request
with urllib.request.urlopen('http://www.python.org/') as f:
print(f.read(300))

The connection gets closed automatically when it comes out of the context manager block defined with with keyword.

When it is required to close a file in Python?

If you use a context manager ( which is what the "with.." statement is) then you don't need to use the .close.

Python manages the resources for you in this case. This is a good article that goes into the detail of how it works.

Its good practice to use context managers whenever possible, and you can create your own using the contextlib library.



Related Topics



Leave a reply



Submit