0

I'm trying to download images with shutil/urlopen because of deprecated? I'm not sure if its deprecation, but urlretrieve doesn't download the file, it just creates folder of the image name instead. After looking at other question I saw one that provides this code, but I get an error on this one too.

from urllib2 import urlopen
from shutil import copyfileobj


url = 'http://www.watchcartoononline.com/thumbs/South-Park-Season-14-Episode-11-Coon-2-Hindsight.jpg'
path = 'image.jpg'

with urlopen(url) as in_stream, open(path, 'wb') as out_file:
    copyfileobj(in_stream, out_file)

output

with urlopen(url) as in_stream, open(path, 'wb') as out_file:
AttributeError: addinfourl instance has no attribute '__exit__
Brandon Nadeau
  • 3,568
  • 13
  • 42
  • 65

3 Answers3

2

urlopen does not implement a context manager, so you cannot use it in a with block. Here is the bug report.

You could use contextlib.closing to wrap it, although the bug report above mentions some issues with that too.

NOTE: this applies only to Python < 3.2

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
2

Try this:

import urllib
urllib.urlretrieve("http://url/img.jpg", "img.jpg")
elyase
  • 39,479
  • 12
  • 112
  • 119
1

urlopen isn't a context manager in Python 2 (I don't know about 3). You have to manually open and close it:

in_stream = urlopen(url)

with open(path, 'wb') as out_file:
    copyfileobj(in_stream, out_file)

in_stream.close()

You can also just use urllib.urlretrieve:

import urllib

urllib.urlretrieve(url, path)

It reads/writes in chunks, which lets you download large files cleanly.

Blender
  • 289,723
  • 53
  • 439
  • 496