尝试从视图写入文件时权限被拒绝 [英] Permission denied when trying to write to file from a view

查看:27
本文介绍了尝试从视图写入文件时权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个问题创建了一个聊天:这里

我有一个视图试图执行 f = open('textfile.txt', 'w') 但在我的实时服务器上,这带来了错误 [Errno 13] 权限拒绝:'textfile.txt'.

我的文件结构如下:

- 根|- 项目|- 应用程序|- 媒体

视图在 app 中的位置.

我曾尝试让 textfile.txt 存在于 root、项目、应用程序和媒体中,所有这些都具有 777 个文件权限(所有者、组和公众可以读取、写入和执行)[*1].

如果我将命令更改为读取权限,即 f = open('textfile.txt', 'r') 我得到同样的错误.

我的媒体根设置为 os.path.join(os.path.dirname(__file__), 'media').replace('\','/')而这一切都是通过 webfaction 在 apache 服务器上运行的.

所以我有两个问题.django/python 试图从哪里打开这个文件?以及我需要更改什么才能获得视图打开和写入文件的权限.

[*1] 我知道这不是一个好主意,我只是为了当前的调试目的设置了这个.


我不知道这是否相关,但现在当我将其更改为 f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'r') 而不是 f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'w') 我收到错误 [Errno 2] No such file or目录.

我不知道这是否有意义...

解决方案

鉴于以下内容:

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

应该在与 __file__、当前运行的脚本或 views.py 相同的目录中创建文件.>

然而,最好是明确的,因此排除任何潜在的偏差.我建议将该行更改为:

导入操作系统f = open(os.path.join(os.path.dirname(__file__), 'textfile.txt'), 'w')

或者甚至更好,例如:

导入操作系统从 django.conf 导入设置f = open(os.path.join(settings.MEDIA_ROOT, 'textfile.txt'), 'w')

然后,您始终可以确定确切文件的保存位置,这应该允许您更适当地优化您的权限.或者,您可以使用 PROJECT_ROOT.

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...

解决方案

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.

这篇关于尝试从视图写入文件时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆