我正在运行一个从网站下载文件的程序。我已经介绍了一个异常处理 urllib.error.HTTPError,但现在我时常会遇到一些我不确定如何捕获的其他错误:http.client.IncompleteRead。我是否只需将以下内容添加到底部的代码中?
except http.client.IncompleteRead:
我必须添加多少个例外以确保程序不会停止?我是否必须将它们全部添加到同一个除外语句或几个除外语句中。
try:
# Open a file object for the webpage
f = urllib.request.urlopen(imageURL)
# Open the local file where you will store the image
imageF = open('{0}{1}{2}{3}'.format(dirImages, imageName, imageNumber, extension), 'wb')
# Write the image to the local file
imageF.write(f.read())
# Clean up
imageF.close()
f.close()
except urllib.error.HTTPError: # The 'except' block executes if an HTTPError is thrown by the try block, then the program continues as usual.
print ("Image fetch failed.")