Is there a way to check if a folder exists on a web server with Python? (something like "os.path.isdir" but reading HTTP responses as "HTTP/404" if not found)
Asked
Active
Viewed 2,956 times
3
-
1There's no such thing as a folder over HTTP. A form of this (collections) were added in the WebDAV extension. Did you mean this? Without webdav, no folders though.. – Evert May 30 '12 at 18:41
2 Answers
4
You can check if a server is serving a file by requesting it in urllib and seeing if it was successfull with a 200 error or unsuccessful.
I think it's difficult to see what is a directory and what isn't. What is a directory though? With modern web frameworks people can route urls however they want. Are you defining a a directory as anything that has a webpage underneath of it even though it might not be hierarchical order on the filesystem?
import urllib
f = urllib.urlopen('http://stackoverflow.com/questions/10822223/check-if-folder-exists-in-a-webserver-using-python-and-http-functions/')
if f.code == 200:
#success

dm03514
- 54,664
- 18
- 108
- 145
0
import requests
r = requests.get('http://stackoverflow.com/questions/10822223/check-if-folder-exists-in-a-webserver-using-python-and-http-functions')
if r:
print "Exists"
else:
print "Doesn't exist"

Maria Zverina
- 10,863
- 3
- 44
- 61