Permission denied when trying to write to file from a view

15,408

Given the following:

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

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

However, it's better to be explicit, and therefore rule out any potential deviations. I'd recommend changing that line to:

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

Or even better, something like:

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

Then, you're always assured exactly where the file is being saved, which should allow you to optimize your permissions more appropriately. Alternatively, you can use a PROJECT_ROOT.

Share:
15,408
Daniel Nill
Author by

Daniel Nill

I like Python, Javascript, Go, Ruby and Beer. Contact at Daniel.L.Nill at gmail dot com

Updated on June 13, 2022

Comments

  • Daniel Nill
    Daniel Nill almost 2 years

    I've created a chat for this question: here

    I have a view that attempts to execute f = open('textfile.txt', 'w') but on my live server this brings up the error [Errno 13] Permission denied: 'textfile.txt'.

    My file structure is as follows:

    - root
        |
        - project
              |
              - app
              |
              - media
    

    where the view lives in app.

    I have tried having textfile.txt live in root, project, app and media all of which have 777 file permissions (owner, group and public can read, write and execute)[*1].

    If I change the command to a read permission ie f = open('textfile.txt', 'r') I get the same error.

    My media root is set to os.path.join(os.path.dirname(__file__), 'media').replace('\\','/') and this is all running on an apache server through webfaction.

    So I have two questions. Where is django/python trying to open this file from? and what do I need to change to get permission for the view to open and write to the file.

    [*1] I know this is not a good idea, I just have this set for current debugging purposes.


    EDIT:

    I don't know if this is relevant but now when I change it to f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'r') rather than f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'w') I get the error [Errno 2] No such file or directory.

    I don't know if this has meaning or not...

    • Ivan Pereira
      Ivan Pereira over 12 years
      have you tried to go with a python console and try to write to file?
    • Daniel Nill
      Daniel Nill over 12 years
      yep, no problem writing to the file from manage.py shell. Note when I write to the file it finds it in the project directory.
    • Ivan Pereira
      Ivan Pereira over 12 years
      and the user that owns the webserver process is the same as you tried to do manage.py shell
    • Daniel Nill
      Daniel Nill over 12 years
      @balsagoth I don't know, how would I check/set that?
    • Ivan Pereira
      Ivan Pereira over 12 years
      How do you installed your django app? With Apache? you can check all processes using for instance wsgi with ps aux | grep wsgi Another suggestion that comes to my mind is to start your django app from ./manage.py runserver
    • Daniel Nill
      Daniel Nill over 12 years
      ah yes. The app is running through wsgi while I suppose the shell would be running without the wsgi router.
    • Daniel Nill
      Daniel Nill over 12 years
  • Daniel Nill
    Daniel Nill over 12 years
    this helps to narrow it down a bit, but all the permissions on the path to my media directory are set and I'm still running into the error. This leads me to think it might have to do with my WSGI script as I can write to the file from the python shell.
  • Daniel Nill
    Daniel Nill over 12 years
    This answer pretty much solved my problem, just make sure to use MEDIA_ROOT rather than MEDIA_URL. I'll edit your answer and accept it once the edit is approved.
  • keramat
    keramat almost 3 years
    Does not work for me. How should one set permissions?