我如何修复“PermissionError:[Errno 13]权限被拒绝:'静态'”?

时间:2018-03-30 19:04:06

标签: flask apache2 mod-wsgi wsgi permission-denied

我正在运行一个带有flask和mod_wsgi的apache2服务器。

在我的烧瓶脚本中,我有一个创建新目录的函数:

def updateDir():
    dbData = News.query.all()
    for row in dbData:
        rowLink = createLink(row.id, row.title)
        finalPath = "static/img/posts/{}".format(rowLink)
        os.makedirs(finalPath, exist_ok=True)

当我尝试运行脚本时,它会显示此回溯:

ERROR in app: Exception on /refresh [GET]
 Traceback (most recent call last):
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
     response = self.full_dispatch_request()
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
     rv = self.handle_user_exception(e)
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
     reraise(exc_type, exc_value, tb)
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
     raise value
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
     rv = self.dispatch_request()
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
     return self.view_functions[rule.endpoint](**req.view_args)
   File "/var/www/abc/abc/main.py", line 133, in refresh
     updateDir()
   File "/var/www/abc/abc/main.py", line 77, in updateDir
     os.makedirs(finalPath, exist_ok=True)
   File "/usr/local/lib/python3.6/os.py", line 210, in makedirs
     makedirs(head, mode, exist_ok)
   File "/usr/local/lib/python3.6/os.py", line 210, in makedirs
     makedirs(head, mode, exist_ok)
   File "/usr/local/lib/python3.6/os.py", line 210, in makedirs
     makedirs(head, mode, exist_ok)
   File "/usr/local/lib/python3.6/os.py", line 220, in makedirs
     mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: 'static'

我已经使用www-data权限写入'static'文件夹,但仍然显示错误。

如果我没有涵盖所有内容以帮助我,请随时提问。

1 个答案:

答案 0 :(得分:0)

不要使用相对路径名。该进程的当前工作目录通常不在您的代码所在的位置。您需要计算相对于某个锚点的绝对路径。参见: