python - "[Errno 13] Permission denied"仅在 Ubuntu 服务器上尝试更新/更改文档时

标签 python django linux ubuntu virtualenv

在 Django 应用程序的管理后端更改/更新信息时,我遇到了权限错误。

有几点需要注意: - 只有当我尝试上传文档或更改包含文档的条目时才会发生。 - 添加/编辑没有附加文件的数据库条目是可以的! - 在我的本地版本上,一切正常。

对我来说,下一个合乎逻辑的步骤是检查文件/文件夹权限, 所以我在我的测试服务器'lrwxrwxrwx'上更改了所有内容,包括上传文件夹,Python3.5的entirty(我在没有解决它之后改回来,我知道保留所有权限是非常冒险的)。

[Errno 13] Permission denied
Request Method: POST
Request URL:    https://admin/inhouse/document/1704/change/
Django Version: 1.11
Exception Type: PermissionError
Exception Value:    
[Errno 13] Permission denied
Exception Location: /usr/lib/python3.5/subprocess.py in _execute_child, line 1551
Python Executable:  /home/ubuntu/.virtualenv/bin/python
Python Version: 3.5.2
Python Path:    
['/home/ubuntu/.virtualenv/lib/python35.zip',
 '/home/ubuntu/.virtualenv/lib/python3.5',
 '/home/ubuntu/.virtualenv/lib/python3.5/plat-x86_64-linux-gnu',
 '/home/ubuntu/.virtualenv/lib/python3.5/lib-dynload',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/home/ubuntu/.virtualenv/lib/python3.5/site-packages',
 '/home/ubuntu/foodlegal/foodlegal-repo',
 '/home/ubuntu/foodlegal',
 '/home/ubuntu/.virtualenv/lib/python3.5/site-packages/IPython/extensions']

这就是导致整个问题的函数

@receiver(post_save, sender=Document)
def convert_pdf_upload(sender, instance, **kwargs):

    if not instance.document_file:
        return

    current_path = settings.BASE_DIR+'/foodlegal/'+instance.document_file.url
    pdf_path = settings.BASE_DIR+'/foodlegal/uploads/documents/'+str(instance.id)+'.pdf'
    swf_path = settings.BASE_DIR+'/foodlegal/uploads/documents/'+str(instance.id)+'.swf'
    txt_path = settings.BASE_DIR+'/foodlegal/uploads/documents/'+str(instance.id)+'.txt'

    pdf2swf_path = settings.BASE_DIR+'/foodlegal/tools/pdf2swf'
    pdftotext_path = settings.BASE_DIR+'/foodlegal/tools/pdftotext'

    try:
        os.rename(current_path, pdf_path)
    except OSError:
        pass
    subprocess.call([pdf2swf_path, pdf_path, '-o', swf_path, '-f', '-T', '9', '-t', '-s',
                    'storeallcharacters'])

    subprocess.call([pdftotext_path, pdf_path])
    txt = codecs.open(txt_path, "r", encoding='utf-8', errors='ignore')
    file_txt = txt.read()
    instance.file_text = re.sub('[^A-Za-z0-9 ]+', ' ', file_txt)

    if instance.content_type in [1, 2]:
        instance.file_text = re.sub('[^A-Za-z0-9 ]+', ' ', instance.document_content)

    post_save.disconnect(convert_pdf_upload, sender=Document)
    instance.save()
    post_save.connect(convert_pdf_upload, sender=Document)

服务器版本:Apache/2.4.18 (Ubuntu) 经销商 ID:Ubuntu 描述:Ubuntu 16.04.2 LTS 发布:16.04 代号:xenial

尝试通过 chmod 777 将权限更改为包括 Python3.5、整个 InHouse Django 应用程序和上传文件夹在内的文件夹,但未成功。

它很可能是权限 chmod 修复类型的东西,但我错过了什么文件夹?

Here is what the traceback calls look like

最佳答案

您需要使 /foodlegal/tools 中的脚本可执行,以便像这样通过 subprocess.call 与它们交互。在该目录中:

chmod +x pdf2swf
chmod +x pdftotext

(那些上面有 .sh 吗?)

进一步阅读:Can't execute shell script from python subprocess: permission denied

关于python - "[Errno 13] Permission denied"仅在 Ubuntu 服务器上尝试更新/更改文档时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56554075/

相关文章:

python - Pandas dataframe - 基于简单计算创建新列

python - 为什么我的标签和条目在 python tkinter 中不可见?

python - Django:如何 'scrub' 用户列表

python - 有没有一种类似于这种方式的方法来在 django 中保存文件(.docx、pdf...)?

python - 将 html 表单数据获取到基于 django 类的 View 中

android - 是否可以将共享库模块保存在 python 中的单独目录中

c - 两兄弟怎么联系?

python - 如何编写从 python 到 ruby​​ 的源代码转换器?

python - 在 Google App Engine Datastore 中按日期过滤

linux - 将 32 位寄存器地址传递给 I2C_RDWR