I have produced a chat with this question: here

I've got a view that tries to execute f = open('textfile.txt', 'w') but on my small live server this raises the mistake [Errno 13] Permission denied: 'textfile.txt'.

My file structure is the following:

- root
    |
    - project
          |
          - app
          |
          - media

in which the view lives in app.

I've attempted getting textfile.txt reside in root, project, application and media which have 777 file permissions (owner, group and public can see, write and execute)[*1].

Basically alter the command to some read permission ie f = open('textfile.txt', 'r') I recieve exactly the same error.

My media root is placed to os.path.join(os.path.dirname(__file__), 'media').replace('\\','/') which is all running with an apache server through webfaction.

And So I have two questions. Where's django/python attempting to open this file from? and what should i switch to get permission for that view to spread out and email the file.

[*1] I understand this really is not recommended, I simply have this looking for current debugging reasons.


EDIT:

I'm not sure if this sounds like relevant however after i change it out to f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'r') instead of f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'w') I recieve the mistake [Errno 2] No such file or directory.

I'm not sure if the has meaning or otherwise...

Because of the following:

f = open('textfile.txt', 'w')

It should be creating the file in same directory as __file__, the presently running script or views.py inside your scenario.

However, it's easier to be explicit, and for that reason eliminate any potential diversions. I'd recommend altering that line to:

import os
f = open(os.path.join(os.path.dirname(__file__), 'textfile.txt'), 'w')

As well as, something similar to:

import os
from django.conf import settings
f = open(os.path.join(settings.MEDIA_ROOT, 'textfile.txt'), 'w')

Then, you are always assured exactly in which the file has been saved, that ought to permit you to optimize your permissions more properly. Alternatively, use a PROJECT_ROOT.