1

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 file creation time. But I need two improvement

1.But what if 2 files are created in the same time stamp? 2.I want to skip the file which is already read(in case no new file arrives) when the while loop is checking for the latest file.

import os
import time

def detect_suspects(file_path, word_list):
    with open(file_path) as LogFile:
        Summary = {word: [] for word in word_list}
        failure = ':'
        for num, line in enumerate(LogFile, start=1):
            for word in word_list:
                if word in line:
                    failure += '<li>' + line + '</li>'
    return failure

while True:
 
 files = os.listdir('.')
 latest_file = max(files, key=os.path.getmtime)
 Error_Suspects = ['Error', 'ERROR', 'Failed', 'Failure']
 print(latest_file)   
 Result = detect_suspects(latest_file, Error_Suspects)
 print (Result)
 time.sleep(5)    
 




 
Andy
  • 405
  • 1
  • 5
  • 16
  • You'll have to fix your indentation... – cs95 Jul 21 '17 at 11:48
  • Hi @COLDSPEED..its working in my IDE..where I have fixed it. What I need now is that I don't want to read the file if it has been already read. By that I mean when the while loop is checking for the latest file it should not read the max file if a newer one have not arrived... – Andy Jul 21 '17 at 11:56

1 Answers1

0

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.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • @COLDSPEED..Thanks again..but getting this error.AttributeError: 'module' object has no attribute 'mtime'..I have imported time – Andy Jul 21 '17 at 12:11
  • @AniruddhyaDutta Sorry... it's `os.path.getmtime`. I forgot the "get". – cs95 Jul 21 '17 at 12:14
  • @COLDSPEED worked like a charm...Thanks a ton...I really do appreciate your efforts for helping me out – Andy Jul 21 '17 at 12:30
  • @AniruddhyaDutta No problem, glad to help! – cs95 Jul 21 '17 at 13:32
  • @COLDSPEED Something I missed...I can understand that "The first string that appears in the list that is associated with the max modification time is returned "that you have stated in your answer.But is there any way I can parse both the files so that I don't miss any? – Andy Jul 22 '17 at 09:02
  • @AniruddhyaDutta This may help: https://stackoverflow.com/questions/9853302/using-pythons-max-to-return-two-equally-large-values – cs95 Jul 22 '17 at 09:04