Need Improvement In The While Loop In Python Program
with some help of from this forum (@COLDSPEED...Thanks a lot )I have been able to read the latest file created in the directory. The program is looking for the max timestamp of fi
Solution 1:
To address your first question, when 2 files have the exact same timestamp, max picks one and returns it. The first string that appears in the list that is associated with the max modification time is returned.
For your second question, you could make a small addition to your existing code by keeping track of the previous file and previous modification time.
Error_Suspects = ['Error', 'ERROR', 'Failed', 'Failure']
prev_file = None
prev_mtime = None
while True:
files = os.listdir('.')
latest_file = max(files, key=os.path.getmtime)
if latest_file != prev_file or (latest_file == prev_file and prev_mtime != os.path.getmtime(latest_file):
Result = detect_suspects(latest_file, Error_Suspects)
prev_file = latest_file
prev_mtime = os.path.getmtime(latest_file)
time.sleep(5)
In this code, the if
condition will execute your code only if 1) your new file is different from your old file, or 2) your old and new file is the same but it was modified since the last time.
Post a Comment for "Need Improvement In The While Loop In Python Program"