The requests.codes.ok equals 200, so you can substitute the exact status code if you wish.
requests.head may throw an exception if server doesn't respond, so you might want to add a try-except construct.
Also if you want to include codes 301 and 302, consider code 303 too, especially if you dereference URIs that denote resources in Linked Data. A URI may represent a person, but you can't download a person, so the server will redirect you to a page that describes this person using 303 redirect.
Try it with mechanize:
The code below is equivalent to tikiboy's answer, but using a high-level and easy-to-use requests library.
The
requests.codes.ok
equals200
, so you can substitute the exact status code if you wish.requests.head
may throw an exception if server doesn't respond, so you might want to add a try-except construct.Also if you want to include codes
301
and302
, consider code303
too, especially if you dereference URIs that denote resources in Linked Data. A URI may represent a person, but you can't download a person, so the server will redirect you to a page that describes this person using 303 redirect.This might be good enough to see if a url to a file exists.
I think you can try send a http request to the url and read the response.If no exception was caught,it probably exists.