Check for `urllib.urlretrieve(url, file_name)` Completion Status

python, urllib

Solution

Requests is nicer than urllib but you should be able to do this to synchronously download the file:

import urllib
f = urllib.urlopen(imgUrl)
with open("tides.gif", "wb") as imgFile:
    imgFile.write(f.read())
# you won't get to this print until you've downloaded
# all of the image at imgUrl or an exception is raised
print "Got it!"

The downside of this is it will need to buffer the whole file in memory so if you're downloading a lot of images at once you may end up using a ton of ram. It's unlikely, but still worth knowing.

Problem

How do I check to see if `urllib.urlretrieve(url, file_name)` has completed before allowing my program to advance to the next statement? Take for example the following code snippet: ``` import traceback import sys import Image from urllib import urlretrieve try: print "Downloading gif....." urlretrieve(imgUrl, "tides.gif") # Allow time for image to download/save: time.sleep(5) print "Gif Downloaded." except: print "Failed to Download new GIF" raw_input('Press Enter to exit...') sys.exit() try: print "Converting GIF to JPG...." Image.open("tides.gif").convert('RGB').save("tides.jpg") print "Image Converted" except Exception, e: print "Conversion FAIL:", sys.exc_info()[0] traceback.print_exc() pass ``` When the download of 'tides.gif' via `urlretrieve(imgUrl, "tides.gif")` takes longer than `time.sleep(seconds)` resulting in an empty or not-complete file, `Image.open("tides.gif")` raises an `IOError` (due to a tides.gif file of size 0 kB). How can I check the status of `urlretrieve(imgUrl, "tides.gif")`, allowing my program to advance only after the statement has been successfully completed?

Original source