Forums

"ValueError: max() arg is an empty sequence" / Using glob.glob / On Python Flask

Good day.

I have a python web app flask. In my app, my goal is to upload a file from a remote machine to the pythonanywhere machine. The file then will be stored on a directory where I would like to put to an object in order to use for email sending automation in python.

I've used the following syntax:

htmlfile = glob.glob("/var/www/flask_app/static/files/*.html")
receiver = glob.glob("/var/www/flask_app/static/files/*.csv")
latest_file_html = max(htmlfile, key=os.path.getctime)
latest_file_rec = max(receiver, key=os.path.getctime)

Error:

ValueError: max() arg is an empty sequence

Investigation checking

  • I tested the functionality on a seperate file the glob.glob together with the syntax " max(path, key=os.path.getctime)" python anywhere environment and it works.

  • But going back with my actual whole code its not working.

Here's the actual error:

Traceback (most recent call last):
  File "/home/mariondeguzman/.virtualenvs/first-flaskapp/lib/python3.9/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/mariondeguzman/.virtualenvs/first-flaskapp/lib/python3.9/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/mariondeguzman/.virtualenvs/first-flaskapp/lib/python3.9/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/mariondeguzman/.virtualenvs/first-flaskapp/lib/python3.9/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/var/www/flask_app/app.py", line 83, in automation_page

My suggestions would be: - double check if there are files matching those glob patterns - if, so, make sure that they do exist when the web app code is being executed (e.g. that you don't have some other code that cleares those files) - you may also add some logging to the view to get more information about the state of the code when it's being executed

Hi pafk, appreciate your inputs.

I've resolved my issue from the insights you've given.

This is what I've done:

~Since this is app is on dev phase, there are no other files on my dedicated directory storage (in python anywhere infra), until I upload something on it. ~The input from pafk: "make sure that they do exist when the web app code is being executed", made me think to add some default files on the dedicated directory that stays there no matter what. ~Upon uploading new files, which are the files that I would need, (reason why I use getctime), it would not encounter the error anymore probably due to satisfying the idea of having "existing files on the directory".

Thank you again pafk and hopefully this forum will help others as well.

Glad to hear that!