I’m new to Python and reading someone else’s code:
should urllib.urlopen() be followed by urllib.close()? Otherwise, one would leak connections, correct?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
closemethod must be called on the result ofurllib.urlopen, not on theurllibmodule itself as you’re thinking about (as you mentionurllib.close— which doesn’t exist).The best approach: instead of
x = urllib.urlopen(u)etc, use:The
withstatement, and theclosingcontext manager, will ensure proper closure even in presence of exceptions.