使用 Flask 访问 Python3.6 上的文件夹的权限被拒绝

Permission denied for access to folder on Python3.6 with Flask

编辑:完全忽略这个问题。这是一个文件系统问题。

每当我尝试打开 posts 文件夹时,我都会收到错误消息:

Traceback (most recent call last):
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36-32\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36-32\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python36-32\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\vikas\Projects\blg\main.py", line 19, in index
    return render_template("index.html", newest_post=find_recent_post())
  File "C:\Users\vikas\Projects\blg\main.py", line 12, in find_recent_post
    with open(str(find_recent_post_name())) as post:
PermissionError: [Errno 13] Permission denied: 'posts/'

我的 main.py 文件:

from flask import Flask
from flask import render_template

app = Flask(__name__)

def find_recent_post_name():
    import glob
    posts = glob.glob("posts/")
    return posts[len(posts) - 1]

def find_recent_post():
    with open(str(find_recent_post_name())) as post:
        newest_post = post.readlines()
    return newest_post


@app.route("/")
def index():
    return render_template("index.html", newest_post=find_recent_post())

if __name__ == '__main__':
    app.run(debug=True)

我的 Python 版本是 v3.6.1 和 Flask v1.12.0

编辑:调试代码和使用 PowerShell 似乎可行。

这不是烧瓶的问题。您可以更改文件夹 /posts/ 的权限,或者您可以 运行 flask 作为具有 read 访问权限的用户。

$ chmod 755 <folder names>

向 运行 的 flask 应用程序的用户授予访问权限,或者您可以使用:

$ sudo python main.py

以 运行 作为 admin 或 运行 flask 作为已经具有访问权限的用户。并注意您应该在部署时添加相同的设置。因此 uwsgigunicron 的工作人员,或者您 运行 您部署的应用程序应该 运行 使用相同的凭据。

请注意,您的 user 应该同时具有 read4 访问权限以读取文件内容,并且具有 execute1 访问权限以能够cd进去。