【发布时间】:2012-07-21 19:01:29
【问题描述】:
在允许我的程序前进到下一条语句之前,如何检查urllib.urlretrieve(url, file_name) 是否已完成?
以如下代码sn-p为例:
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
当通过urlretrieve(imgUrl, "tides.gif") 下载'tides.gif' 的时间超过time.sleep(seconds) 导致文件为空或不完整时,Image.open("tides.gif") 会引发IOError(由于潮汐.gif 文件的大小0 KB)。
如何查看urlretrieve(imgUrl, "tides.gif")的状态,让我的程序只有在语句成功完成后才能前进?
【问题讨论】: