Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a file on http exists, using Python/django?

Tags:

python

How do I check if a file on http exists, using Python/Django?

I try to check if file in http://hostname/directory/file.jpg exist

like image 993
Nips Avatar asked Nov 29 '22 16:11

Nips


1 Answers

Try urllib2.urlopen:

import urllib2
ret = urllib2.urlopen('http://hostname/directory/file.jpg')
if ret.code == 200:
    print "Exists!"

Note that you don't check if file exists - you check if resource exists

EDIT: The other answer by user Geo is better in that HEAD request can be much more efficient as it doesn't fetch resource content.

like image 91
Tomasz Zieliński Avatar answered Dec 08 '22 14:12

Tomasz Zieliński