Skip to content

Instantly share code, notes, and snippets.

@psykzz
Last active December 17, 2015 00:10
Show Gist options
  • Save psykzz/5518676 to your computer and use it in GitHub Desktop.
Save psykzz/5518676 to your computer and use it in GitHub Desktop.
file monitor
import os
import sys
import time
class LogReader():
def __init__(self):
self.defaultPath = os.path.join(os.environ['USERPROFILE'],"Documents","EVE","logs","Gamelogs")
self._fp = None # File Handle
self.lastFile = None
self.open()
def open(self,customPath=False):
if not customPath:
customPath = self.defaultPath
self.getLastModifiedFiles()
self.lastFile
self._fp = open(self.lastFile)
def getLastModifiedFiles(self):
os.chdir(self.defaultPath)
files = os.listdir(self.defaultPath)
files = filter(os.path.isfile, os.listdir(self.defaultPath))
files = [os.path.join(self.defaultPath, f) for f in files] # add path to each file
files.sort(key=os.path.getmtime, reverse=True)
self.lastFile = files[0]
def tail(self,interval):
while True:
oldFile = self._fp
where = oldFile.tell()
line = oldFile.readline()
if not line:
self.getLastModifiedFiles()
# If no change, goto end of file and try again...
if oldFile is self._fp:
time.sleep(interval)
oldFile.seek(where)
else:
# Make sure the new file is at the start.
self._fp.seek(0, 0)
else:
yield line
'''
def doStuff(self):
for line in self._fp:
if "combat" in line:
print line
'''
if __name__ == '__main__':
reader = LogReader()
reader.open()
#while reader.doStuff():
# pass
for line in reader.tail(1):
print line,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment