Search code examples
flaskfile-permissionspython-3.6

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


EDIT: Ignore this question entirely. It was a filesystem problem.

Whenever I try to open the posts folder, I get an error:

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/'

My main.py file:

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)

My Python version is v3.6.1 and Flask v1.12.0

EDIT: Debugging code and using PowerShell seems to work.


Solution

  • It is not a problem with flask. you can either change the permissions on the folder /posts/ or you can run flask as a user who has read access.

    $ chmod 755 <folder names>
    

    to give access to the user who run's flask app, or you could use:

    $ sudo python main.py
    

    to run as admin or run flask as a user who already has access. and note that you should add the same settings while on deployment. so workers for uwsgi or gunicron or however you run your app on deployment should run with same credentials.

    note that your user should have both read or 4 access to read the file content's, and have execute or 1 access to be able to cd into it.