I have some code that will find the newest file in a directory and append a time stamp to the file name. It works great as long as there is a file in the directory to rename. If there isn't I am getting:
"ValueError: max() arg is an empty sequence"
Here's my code:
import os
import glob
import datetime
now = datetime.datetime.now()
append = now.strftime("%H%M%S")
newest = max(glob.iglob('1234_fileName*.LOG'), key=os.path.getmtime)
newfile = (append+"_"+newest)
os.rename(newest, newfile)
Any suggestions for simplifying the code would be appreciated as well as explaining how to only run if a "1234_fileName*.LOG" (note the wildcard) file is detected.
What I need this program to do is run periodically (I can use task scheduler for that) and check for a new file. If there is a new file append the hours, minutes and seconds to it's name.
Thanks!