Forums

Permission denied for '/static'

I am trying to deploy an app and it's static files aren't accessible from the flask_app.py directly. Could you suggest why?

Error:

  File "/home/certigeneratorvvp/mysite/flask_app.py", line 126, in index
    generateCertificates(data)
  File "/home/certigeneratorvvp/mysite/flask_app.py", line 70, in generateCertificates
    os.makedirs(OUTPUT_IMAGES_PATH, exist_ok=True)
  File "<frozen os>", line 215, in makedirs
  File "<frozen os>", line 215, in makedirs
  File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/static'

Code:

TEMPLATE_PATH = "/home/certigeneratorvvp/mysite/static/img/template.png"

OUTPUT_IMAGES_PATH = '/home/certigeneratorvvp/mysite/static/output/img'

def generateCertificates(data):

    # This function generates certificates based on the provided data
    print(data.keys())
    os.makedirs(OUTPUT_IMAGES_PATH, exist_ok=True)

    #Load the template image
    template = Image.open(TEMPLATE_PATH).convert("RGB")

    #Load the Drawing context
    draw = ImageDraw.Draw(template)

    #Load the font
    font = ImageFont.truetype(FONT_PATH, FONT_SIZE)

    draw.text(TEXT_POSITION, data['Name'], font=font, fill=FONT_COLOR)

    output_file = f"{OUTPUT_IMAGES_PATH}/{data['Name'].replace(' ', '_')}_certificate.jpg"
    template.save(output_file)
    print(f"Certificate saved to {output_file}")
    return 1

[edit by admin: formatting]

That's a weird error; it's as if it's trying to create a directory called /static rather than the path that you've specified. But perhaps it's just a confusing error message. What do you see if you run:

ls -ld /home/certigeneratorvvp/mysite/

It might also be worth printing out OUTPUT_IMAGES_PATH immediately before the call to makedirs, just in case something elsewhere in your code is unexpectedly changing it.

BTW if you add the flush=True keyword argument to your calls to print, it will flush immediately to disk, so you'll see the results faster.

hello giles Thanks for your suggestion, when I ran that ls commmand it gave me this output

drwxrwxr-x 6 certigeneratorvvp registered_users 4096 May  2 14:51 /home/certigeneratorvvp/mysite/

and when I reloaded the site with print command added it worked completely. I am still confused why it didn't access it earlier but now it does with no error.

Glad to hear that it works now as expected for you.