Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python method to validate existence of file or URL?

Tags:

python

I'm writing a Python script that needs to validate existence of a file. The file can be either a full path like /home/xxx/file.txt or a URL http://company.com/xxx/file.txt.

Is there a python method that can validate existence of various schema of path?

like image 553
TieDad Avatar asked Dec 14 '25 15:12

TieDad


1 Answers

What do you plan to do with the file?

If you need to use the file you might be better off opening it, lest it disappear before you use it. There can be security issues if you test first and then open as the two operations can not be made atomic. It's possible that the file could be removed, created, or otherwise interfered with before your code opens it.

If you simply want to know whether a path exists at the time you test for it use os.path.exists(). Otherwise, if you want to actually do something with the file, call open() on it.

For URLs you need to access it... either GET it with urlopen() or use requests. You could also try sending a HEAD request to determine whether a resource exists without downloading it content. This is useful if you were checking a resource that returns a lot of data, like an image or music file. The requests module makes that easy:

import requests

r = requests.head(url, allow_redirects=True)
if r.status_code == 200:
    # resource apparently exists

The allow_redirects is necessary for HEAD requests, e.g.

import requests

url = 'http://www.google.com'
r = requests.head(url)
print(r.status_code)
# 302
r = requests.head(url, allow_redirects=True)
print(r.status_code)
# 200
like image 141
mhawke Avatar answered Dec 16 '25 04:12

mhawke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!