Accessing files on ubuntu server

I have this simple function.

@app.route('/save')
def save():
    with open("hello.txt", 'w') as file:
        file.write("hello")
        file.close()
    return "done"

      

But when I visit the route www.example.com/save

, the server returns 500 Internal Server Error and the file is not written. What permission should I set for this application Flask

that is in /var/www/

so that the application can write the file to the directory?

The error says: I/O error(13): Permission denied

+3


source to share


1 answer


You need to make sure that the user of the python process (in this case, the Flask app server) has write access to the directory where you want to save the file. For example, if you want to save a file in the / var / www directory, make sure the user of the python process has the correct permissions.

ls -al /var/www
sudo chown flask-user /var/www

      

Also, inside the Flask route, you should probably point to your storage folder instead of saving the file to the working directory of the process (which is what happens in your case). Something like this would work well:



@app.route('/save')
def save():
    with open("/var/www/hello.txt", 'w') as file:
        file.write("hello")
        file.close()
    return "done"

      

Note. You should probably choose a different directory, but / var / www for where your files are stored.

+2


source







All Articles