1

I have a small program through which I am trying to read log files generated in a file location like below

Error_Suspects = ['Error', 'ERROR', 'Failed', 'Failure']


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


Result = detect_suspects(r'C:\scripts\Log.txt', Error_Suspects)

now the issue is this was good until I have only one single file. But now the files are generated at a certain interval with the timestamp as below.

enter image description here

I want to modify the above program in such a way so that it should always check the file with the latest time stamp. Also, I want to loop this program to run every 5 mins to check for the latest file. If new file doesn't arrive within 5 min it should not read the old one (which is already been read)

andras.tim
  • 1,964
  • 1
  • 13
  • 23
Andy
  • 405
  • 1
  • 5
  • 16

1 Answers1

1

If you're in the directory containing all those files, you can use os.path.getmtime to get the latest file.

You'd use it like this:

import os
files = os.listdir('.')
latest_file = max(files, key=os.path.getmtime)

os.listdir returns a list of all files in the current directory. max with the key finds the file with the most recent modification time, and returns it.


To loop over your directory every 5 minutes, you'll need to instate a while loop:

import time
while True:
    Result = detect_suspects(r'C:\scripts\Log.txt', Error_Suspects)
    time.sleep(5 * 60) # 300 seconds or 5 minutes
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Wao!! Thanks for replying so fast...the code did work... I am very new to python..can you help me modifying the code to loop this program to run every 5 sec to check the latest file . – Andy Jul 20 '17 at 18:18
  • @COLDSPEED I have updated my original question with the code.Can you please have a look...if you don't mind – Andy Jul 20 '17 at 18:37
  • @AniruddhyaDutta I'd love to help. But changing the theme of a question constantly is bad form as it reduces its readability for future readers. I urge you to open another one. – cs95 Jul 20 '17 at 18:38
  • @COLDSPEED unfortunately I can post only every 90 miniutes it seems.Can you please help me as of now..I will post once I get some chance again.. – Andy Jul 20 '17 at 18:44
  • Okay... you'll put a while loop, call your function, and sleep for 5 seconds. `import time; while True: Result = detect_suspects(r'C:\scripts\Log.txt', Error_Suspects); time.sleep(5)` – cs95 Jul 20 '17 at 18:45
  • @COLDSPEED can you please look into my edit 2 in the question. When I run its constantly returning the program name in the console.. – Andy Jul 20 '17 at 19:08
  • @AniruddhyaDutta You've implemented it wrong. The fix is simple but a comment is too short to do justice to it ;/ – cs95 Jul 20 '17 at 19:10
  • @COLDSPEED can I get your mail id by any chance? if only its not too much I am asking.. – Andy Jul 20 '17 at 19:17
  • @AniruddhyaDutta My email is in my profile but I don't answer questions offline. :) – cs95 Jul 20 '17 at 19:17
  • I get you..but I have wait again till I post a new question where you can answer...is it like someone cant post an improvement once its got accepted..? – Andy Jul 20 '17 at 19:20
  • @AniruddhyaDutta Not sure... I haven't been in this position before. Try now? – cs95 Jul 20 '17 at 19:21
  • I think you can edit and improve your original answer..I see there is an option to edit @COLDSPEED – Andy Jul 20 '17 at 19:25
  • @AniruddhyaDutta Editing my answer is fine, but like I said, you shouldn't pollute a question once it has been closed. Just wait a few more minutes! – cs95 Jul 20 '17 at 19:28
  • @AniruddhyaDutta Also recommend when you can, rollback your edit so as to leave out details that don't concern themselves with this question. – cs95 Jul 20 '17 at 19:28
  • I totally agree with you..but this looping thing was also mentioned in my original question..so if you just take it as an exception today...:( it seems like I am in the brink of getting this resolved ..its only that I have to wait to post a new question...@coldspeed – Andy Jul 20 '17 at 19:30
  • @COLDSPEED...Thanks a lot...what a silly mistake I was making..but as promised I have posted a new question...can you look into that whenever you get some time? https://stackoverflow.com/questions/45232362/need-improvement-in-the-while-loop-in-python-program – Andy Jul 21 '17 at 08:09