2

I know how to download a file from the web using python, however I wish to handle cases where the file being requested does not exist. In which case, I want to print an error message ("404: File not found") and not write anything to disk. However, I still want to be able to continue executing the program (i.e. downloading other files in a list that may exist).

How do I do this? Below is some template code to download a file given its url (feel free to modify it if you believe there is a better way, but please keep it concise and simple).

import urllib
urllib.urlretrieve ("http://www.example.com/myfile.mp3", "myfile.mp3")
Josh
  • 1,357
  • 2
  • 23
  • 45

3 Answers3

0
from urllib2 import URLError

try:
    # your file request code here
except URLError, e:
    if e.code == 404:
        # your appropriate code here
    else:
        # raise maybe?

I followed this guide, which has a specific section about handling exceptions, and found it really helpful.

Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
-1

Your code should look like this:

try:
    urllib.urlretrieve ("http://www.example.com/myfile.mp3", "myfile.mp3")
except URLError,e:
    if e.code==404:
        print 'file not found. moving on...'
        pass
Meltim
  • 3
  • 2
dinesh kumar
  • 103
  • 8
  • 3
    From what I can tell, urllib.urlretrieve won't raise a URLError on a 404 response. If the domain is bad it will raise IOError. Otherwise `myfile.mp3` will just contain the html 404 response. – johncip Aug 10 '16 at 20:31
-1
import urllib, urllib2
try:
    urllib.urlretrieve ("http://www.example.com/", "myfile.mp3")
except URLError, e:
    if e.code == 404:
        print "4 0 4"
    else:
        print "%s" % e 

This is what your code does. It basically tries to retrieve the web page of www.example.com and writes it to myfile.mp3. It does not end into exception because it is not looking for the myfile.mp3, it basically writes everything it gets in html to myfile.mp3

If you are looking for code to download files at a certain location on the web, try this

How do I download a zip file in python using urllib2?

Community
  • 1
  • 1
ronak
  • 1,770
  • 3
  • 20
  • 34