dangerChihuahua007
dangerChihuahua007

Reputation: 20915

How come I cannot download an image with urllib.urlretrieve?

I tried to download Google's logo with

import os, urllib
folderName = 'downloadedImages'
if not os.path.exists(folderName):
  os.makedirs(folderName)
urllib.urlretrieve(https://www.google.com/images/srpr/logo3w.png, './' + folderName + '/')

However, I receive an error: IOError: [Errno 21] Is a directory: './downloadedImages/'.

Why?

Upvotes: 0

Views: 2226

Answers (3)

Rohan
Rohan

Reputation: 53386

Second parameter to urllib.urlretrieve() need to a file name rather than directory.

Upvotes: 0

UltraInstinct
UltraInstinct

Reputation: 44454

Check the documentation: http://docs.python.org/library/urllib.html#urllib.urlretrieve

You need to give the filename, not the directory path:

urllib.urlretrieve("path/to/resource", os.path.join(folderName, "filename.jpg"))

Upvotes: 2

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137517

Because of just what the error says: What you've provided in the second argument is a directory. It needs to be the local (destination) filename.

The python docs state (emphasis mine):

The second argument, if present, specifies the file location to copy to (if absent, the location will be a tempfile with a generated name).

Upvotes: 2

Related Questions