As pointed out by Lott, if the download fails, if the problem is not fixed upstream (or with your download address), the best thing you can do is try again. However, if the situation is that you have a list of downloads, and just want to skip failed downloads instead of exiting, then:
logf = open("download.log", "w") for download in download_list: try: # code to process download here except Exception as e: # most generic exception you can catch logf.write("Failed to download {0}: {1}\n".format(str(download), str(e))) # optional: delete local version of failed download finally: # optional clean up code pass
Notes:
(1) Using the " logging " module, as suggested by ~ unutbu, gives you more flexibility and power with log output, including a time stamp, while recording on different channels (e.g. stderr, file) depending on the level of errors, etc. etc.
(2) You may consider implementing the above logic using the " with " construct.
Jeet
source share