简体   繁体   中英

WindowsError: [Error 5] Access is denied in Flask

I am trying to move an uploaded file to a specific folder in my Windows system and it gives me WindowsError: [Error 5] Access is denied error. The solutions I happen to see for such problems are run python as Administrator from cmd line. I am not sure if that is possible since it's a web app and i am using the default flask server for development purpose and run it from Pycharm.

my code is

@app.route('/test',methods=['POST'])
def test():
    import os
    if not os.path.exists("history_plugin"):
        os.makedirs("test")
        f = open('test/abc.txt', 'w+')
        f.close()

Running the application 'directly in pycharm' is the equivlant of running it on the command prompt, but with a few caveats. Personally I don't like running python in pycharm as I find it can cause errors.

Ideally you don't want to run as administrator, but you might find you have a couple of problems when it comes to windows. Firstly are you sure the Access Denied is from the file, and not from the trying to bind the app to port 80 (also note other issues with trying to bind on windows such as Skype taking over port 80)

If the problem is caused by the mkdir, make sure your user has permissions on the parent folder, not just the folder its creating. You are right to be wary of running as admin. Generally speaking you should create users per service and run as that, but it can be a pain during development (also, in 'production' you would want to run something like uwsgi or similar to act as a python process manager).

The other thing to note is that where you are running from - if you're running from your Desktop folder, I have noticed that this can also have strange permission problems for applications - but I'm assuming you're in a user 'workbench' folder of some kind.

I had been running the application directly from Pycharm, which doesn't run it in administrator mode

I tried running it using command prompt as administrator and it worked for me.

if running on windows and inside a python virtual environment "venv "

type pip install flask

should do the trick, you must've been using a global flask installation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM