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...
Right-click the file or folder, and then click Properties. Click the Security tab. Under Group or user names, click your name to see the permissions that you have. Click Edit, click your name, select the check boxes for the permissions that you must have, and then click OK.
This error occurs when the user does not have the privileges to make edits to a file. Root has access to all files and folders and can make any edits. Other users, however, may not be allowed to make such edits. Remember that only root or users with Sudo privileges can change permissions for files and folders.
Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it. It worked for me.
Final Words. The “SFTP permission denied” error message occurs when your SFTP server doesn't allow your user (within a group) to modify or overwrite a file or directory. To solve this, you'll have to SSH into the SFTP server, find the file/directory and identify its current permission mode and ownership.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With