0
votes

I have a python flask app hosted on ubuntu-apache with WSGI-mod. It works perfectly on my local windows machine, but on the ubuntu machine I cannot seem to access './static/images/' through my python script? Everything else works, but this exact piece of code does not for some reason and produces this error.

I've tried searching around and editing the VirtualHost, but nothing seems to work. Can't wrap my head around the problem. Any ideas?

Code that gives error:

if session.get('logged_in'):
        directory = r'./static/images/'
        urls = []
        for filename in os.listdir(directory):
            if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
                urls.append(os.path.join(directory, filename))
            else:
                continue

Error Message:

[Wed Feb 17 10:13:10.857968 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] [2021-02-17 10:13:10,856] ERROR in app: Exception on /cdn-controlpanel [GET], referer: website-uri [Wed Feb 17 10:13:10.858012 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] Traceback (most recent call last):, referer: website-uri [Wed Feb 17 10:13:10.858019 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2447, in wsgi_app, referer: website-uri [Wed Feb 17 10:13:10.858025 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] response = self.full_dispatch_request(), referer: website-uri [Wed Feb 17 10:13:10.858030 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1952, in full_dispatch_request, referer: website-uri> [Wed Feb 17 10:13:10.858035 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] rv = self.handle_user_exception(e), referer: website-uri [Wed Feb 17 10:13:10.858040 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1821, in handle_user_exception, referer: website-uri> [Wed Feb 17 10:13:10.858045 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] reraise(exc_type, exc_value, tb), referer: website-uri [Wed Feb 17 10:13:10.858050 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/_compat.py", line 39, in reraise, referer: website-uri [Wed Feb 17 10:13:10.858055 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] raise value, referer: website-uri [Wed Feb 17 10:13:10.858060 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1950, in full_dispatch_request, referer: website-uri> [Wed Feb 17 10:13:10.858066 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] rv = self.dispatch_request(), referer: website-uri [Wed Feb 17 10:13:10.858071 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1936, in dispatch_request, referer: website-uri> [Wed Feb 17 10:13:10.858076 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] return self.view_functionsrule.endpoint, referer: website-uri [Wed Feb 17 10:13:10.858081 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/var/www/FlaskApp/App/app.py", line 99, in cdnControlpanel, referer: website-uri [Wed Feb 17 10:13:10.858086 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] for filename in os.listdir(directory):, referer: website-uri [Wed Feb 17 10:13:10.858130 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] FileNotFoundError: [Errno 2] No such file or directory: './static/images/', referer: website-uri

1

1 Answers

0
votes

Figured it out. When stating directory-paths in python, the "." does not mean relative to current file, but relative to current working directory which can be changed with os.chdir('path/to/dir')