Search code examples
pythonhttpfile-exists

Check if folder exists in a webserver using python and http functions


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)


Solution

  • 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