urllib2 does not implement "with" Python 2.6

Discussion in 'Python' started by John Nagle, Aug 12, 2010.

  1. John Nagle

    John Nagle Guest

    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
    John Nagle, Aug 12, 2010
    #1
    1. Advertising

  2. John Nagle

    Paul Rubin Guest

    John Nagle <> writes:
    > "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.
    Paul Rubin, Aug 12, 2010
    #2
    1. Advertising

  3. John Nagle

    Terry Reedy Guest

    On 8/12/2010 1:34 AM, John Nagle wrote:
    > 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


    --
    Terry Jan Reedy
    Terry Reedy, Aug 12, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Schulze
    Replies:
    1
    Views:
    569
    Esmond Pitt
    Mar 26, 2005
  2. Josef Cihal
    Replies:
    0
    Views:
    744
    Josef Cihal
    Sep 5, 2005
  3. Sideswipe
    Replies:
    42
    Views:
    1,247
    Patricia Shanahan
    Aug 2, 2007
  4. nxliu
    Replies:
    1
    Views:
    1,095
    Ben Lovell
    Aug 25, 2004
  5. Aseem Bansal
    Replies:
    29
    Views:
    253
    Chris Angelico
    Jul 19, 2013
Loading...

Share This Page