I am trying to find the date of the newest file in a directory, based on some Python code I shamelessly found on this site.
def newest_file_in_tree(rootfnewer, extension=".avi"):
return max(
(os.path.join(dirname, filename)
for dirname, dirnames, filenames in os.walk(rootfnewer)
for filename in filenames
if filename.endswith(extension)),
key=lambda fn: os.stat(fn).st_mtime)
This works, but fails on empty directories.
key=lambda fn: os.stat(fn).st_mtime)
ValueError: max() arg is an empty sequence
I concede that I am a Python beginner, and the last line is way beyond my pay grade.
Can anyone suggest how I can trap the error and prevent the function from crashing?