I wrote a python script to run every 5 minutes using Task Scheduler, read a constantly growing log files (text files) and insert data into DB. New log file generated each day.
I need to modify it and put a pointer at the end of last line, so when the scheduler runs again, it starts after the last inserted line. Once a new day begins, pointer get back to the first line of the new file. Seek function would do it but couldn't figure out how yet. Here is my try:
import time, os
day=time.strftime("%Y%m%d")
month=time.strftime("%m")
filename=time.strftime("%Y%m%d")
# Check for a new day
currTime = datetime.datetime.now()
lastDay = 0
#Open file in a relative location
logs_dir = os.path.dirname(r'C:\Python27\Logs\\')
rel_path = os.path.join('\\', month, filename + '.log')
abs_file_path = os.path.join(logs_dir, month, filename) + '.log'
file = open(abs_file_path, 'r')
if currTime.day != lastDay:
lastDay = currTime.day
file.seek(first_byte_to_read) #<<-- to reset the pointer ??
else:
file.seek(last_read_byte)