tags:

views:

35

answers:

1

I want to download an image file from potentially 5 sites.

Meaning that if the image wasn't found in site#1, try site#2, etc.

How can I test if the file was downloaded?

+3  A: 

You can call getcode() on the object you get back from urlopen().

getcode() gives you the HTTP status response from the server, so you can test to see if you got an HTTP 200 response, which would mean the download was successful.

zerocrates
Plus, urllib should raise an `IOError` when it can't connect or find your file.
zerocrates
It's worth checking the `Content-Type` returned as well to see whether it's `image/something` as opposed to `text/html`. Some horribly misguided hosts may respond to a missing file with something stupid and broken like an error page served with `200 OK`, or a `302` redirect. The fatheads.
bobince
I thought the object returned would be the image?
Blankman
`urlopen()` returns a file-like object, which behaves mostly like reading any other file, but with some differences, including the `getcode()` function. See http://docs.python.org/library/urllib.html#urllib.urlopen
zerocrates