urllib2 does not implement "with" Python 2.6

J

John Nagle

Somewhat to my surprise, in Python 2.6,

with urllib2.urlopen(url) as fh :

doesn't work. It fails with
"AttributeError: addinfourl instance has no attribute '__exit__'".

I thought that all the file-like objects supported "with" in 2.6.
No?

John Nagle
 
P

Paul Rubin

John Nagle said:
"AttributeError: addinfourl instance has no attribute '__exit__'".

I thought that all the file-like objects supported "with" in 2.6.
No?

I guess not. Use contextlib.closing.
 
T

Terry Reedy

Somewhat to my surprise, in Python 2.6,

with urllib2.urlopen(url) as fh :

doesn't work. It fails with
"AttributeError: addinfourl instance has no attribute '__exit__'".

I thought that all the file-like objects supported "with" in 2.6.
No?

This seems to work in 3.1, which first came out almost a year after 2.6.

from urllib.request import urlopen
with urlopen("http://www.python.org") as fh : pass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top