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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…