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

时间:2017-07-03 15:53:31

标签: flask file-permissions python-3.6

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

每当我尝试打开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似乎有效。

1 个答案:

答案 0 :(得分:0)

烧瓶不是问题。您可以更改文件夹/posts/的权限,也可以以read访问权限的用户身份运行flask。

$ chmod 755 <folder names>

允许访问运行烧瓶应用的用户,或者您可以使用:

$ sudo python main.py

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

请注意,您的user应具有read4访问权限,以便阅读文件内容并获得execute1访问权限能够cd进入它。