I've searched for an answer for this but the answers still gave me an error message and I wasn't allowed to ask there because I had to make a new question. So here it goes...
I need my python script to use the latest file in a folder.
I tried several things, currently the piece of code looks like this:
list_of_files = glob.glob('/my/path/*.csv')
latest_file = max(list_of_files, key=os.path.getmtime)
But the code fails with the following comment:
ValueError: max() arg is an empty sequence
Does anyone have an idea why?
list_of_filesis empty you may need to provide a full path or change the working directory firstmax(andmin) in Python 3 has a way of handling empty lists. But it's cleaner to make surelist_of_filesisn't empty before you pass it tomax.maxto see if it is what you expect it to bemaxof each individual timestamp, which doesn't make a lot of sense, andmaxraisesTypeErrorif you try to get the maximum of a number rather than an iterable.