Search code examples
pythondjangopythonanywhere

Cannot access file on pythonanywhere


I have a django project that worked perfectly on my local server returning a response. I am now trying to run it on pythonanywhere, it keeps saying no such directory or file. I initially used the os.path.dirname("__file__") but then I changed it into the absolute address, i.e. "/home/username/projectname/filename" to no avail. That latter method is the only one others on the web are suggesting, but it still isn't working. Is there a special syntax to access files in pythonanywhere? or do you have any suggestions? Thanks.

The following is the line that throws the error:

with open("home/<username>/<project>/layer.pem", "r") as rsa_priv_file:

Directory structure: Directory structure


Solution

  • If this with open("home/<username>/<project>/layer.pem", "r") as rsa_priv_file: is the actual code you're using, then you're missing a / at the beginning. What you're actually asking for with that code is not the absolute path to layer.pem, but a relative path rooted in the current directory.

    Also, the os.path.dirname("__file__") is not working because you quoted the __file__. What you're asking for is the dirname of a file called "__file__" (which will be an empty string), not the dirname of the current file.