应该为目录设置什么权限以避免权限被拒绝?

时间:2014-03-05 00:52:06

标签: python

我在开发模式下使用python manage.py runserver并获取

IOError at /cmanager/upload/save
[Errno 13] Permission denied: u'/tmp/temp/IMG_27022014_183050.png'

运行chmod -R 775 "/tmp/temp/"后,它就可以了。但是在每次关闭/重启计算机时,/ tmp中的目录会自动删除,因为需要手动创建。

settings.py

CONTENT_STORAGE_PATH    /tmp/temp/

控制器

if not os.path.exists(settings.CONTENT_STORAGE_PATH):
    try:
        os.makedirs(settings.CONTENT_STORAGE_PATH, 0644)
    except OSError, e:
       self.raiseException(e)
content_storage_path = os.path.join(settings.\
                                          CONTENT_STORAGE_PATH, f.name)
with open(content_storage_path, 'wb+') as destination:
       for chunk in f.chunks():
         destination.write(chunk)

如何避免此Permission Denied错误。

设置权限是否合适?喜欢:os.chmod(content_storage_path, 0600)。如果是这样应该是什么? 0775

注意:在配置"/tmp/temp/"

时,我将在生产模式下将位置"/var/www/temp/"更改为Apache/NginX

1 个答案:

答案 0 :(得分:1)

在脚本中设置os.chmod权限并不好,因为您无法将权限升级到比进程本身更高的权限。

我认为你根本不应该使用os模块。使用内置tempfile模块进行经过实践检验的跨平台方法,以满足您的需求。

http://docs.python.org/2/library/tempfile.html

如果您的权限问题仍然存在,则需要在脚本之外解决它们 - 它们是环境问题而不是代码的责任。

相关问题