0

I'm wondering if it is possible to do the following thing that is mentioned in the title in python. I managed to write a code to check if the file exists, but i'm stuck at the step, where I want to read the continously changing log file, until a certain string is found in it. The aim is to continue my for loop once that certain string is found in the log file:

Here is the code i managed to write:

list1=[20,40,60]
for i in list1:
    ##some code here
    while not os.path.exists("path.log"):
        time.sleep(1)

    if os.path.isfile("path.log"):
        while True:
             # with open ("path.log") as f:
                if 'string' in open("path.log").read()
                    break
                else 
                    time.sleep(1)
                    continue
    else:
        raise ValueError("%s isn't a file!" % file_path)
Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
G.David
  • 55
  • 6
  • Are you tied to a specific OS, or do you need to produce a code that must work on all platforms ? – Antwane Dec 07 '18 at 09:21
  • 1
    Anyway, you may check [watchdog](https://github.com/gorakhargosh/watchdog) project to be notified when your file change instead of actively polling on it, waiting for this value to be written in it. – Antwane Dec 07 '18 at 09:23
  • It must work on windows only – G.David Dec 07 '18 at 09:37

3 Answers3

0

I've used the answer from here and edited it a bit.

This will follow a file like tail -f and stop when the word STOP is found:

import time
import re

def follow(thefile):
    thefile.seek(0,1)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

if __name__ == '__main__':
    regex = r'\bSTOP\b'
    logfile = open("file.log","r")
    loglines = follow(logfile)
    for line in loglines:
        check = re.search(regex,line)
        if check:
            print 'stop condition met'
            break
edbighead
  • 5,607
  • 5
  • 29
  • 35
0

Since you need to watch for a specific file change on Windows, you may check this SO post which cover this case.

From this post, there is a link to a valid example using Python's win32 API:

import os

import win32file
import win32event
import win32con

path_to_watch = os.path.abspath (".")

#
# FindFirstChangeNotification sets up a handle for watching
#  file changes. The first parameter is the path to be
#  watched; the second is a boolean indicating whether the
#  directories underneath the one specified are to be watched;
#  the third is a list of flags as to what kind of changes to
#  watch for. We're just looking at file additions / deletions.
#
change_handle = win32file.FindFirstChangeNotification (
  path_to_watch,
  0,
  win32con.FILE_NOTIFY_CHANGE_FILE_NAME
)

#
# Loop forever, listing any file changes. The WaitFor... will
#  time out every half a second allowing for keyboard interrupts
#  to terminate the loop.
#
try:

  old_path_contents = dict ([(f, None) for f in os.listdir (path_to_watch)])
  while 1:
    result = win32event.WaitForSingleObject (change_handle, 500)

    #
    # If the WaitFor... returned because of a notification (as
    #  opposed to timing out or some error) then look for the
    #  changes in the directory contents.
    #
    if result == win32con.WAIT_OBJECT_0:
      new_path_contents = dict ([(f, None) for f in os.listdir (path_to_watch)])
      added = [f for f in new_path_contents if not f in old_path_contents]
      deleted = [f for f in old_path_contents if not f in new_path_contents]
      if added: print "Added: ", ", ".join (added)
      if deleted: print "Deleted: ", ", ".join (deleted)

      old_path_contents = new_path_contents
      win32file.FindNextChangeNotification (change_handle)

finally:
  win32file.FindCloseChangeNotification (change_handle)

Alternatively, you may check the python library called Watchdog, that aims to provide this kind of feature on all common platforms (Unix / Windows).

In both case, the idea is to create a script that react on a file change on filesystem. This uses OS specific events / interrupts to notify your script that something may interest you. This is less expansive than actively polling your file (checking for a string every second).

Antwane
  • 20,760
  • 7
  • 51
  • 84
0

Try this

import os
import time

list1 = [20, 40, 60]

for i in list1:
    while not os.path.exists("path.log"):
        time.sleep(1)

    if os.path.isfile("path.log"):
        while True:
            if 'String' in open("path.log").read():
                break
            time.sleep(1)
    else:
        raise ValueError("%s isn't a file!" % file_path)
Shivam Arora
  • 354
  • 2
  • 8