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.