I’m trying to write a something that monitors a log file and adds a timestamp when it sees a complete line:
import sys
f = open(sys.argv[1])
if not f:
print 'Failed to open %s' % sys.argv[1]
print sys.argv[1]
import time
try:
while True:
line = f.readline().replace('\n', '')
if not line:
continue
print time.time(), line
except KeyboardInterrupt:
pass
f.close()
The check for line contents is there because, to my surprise, readlines does not block, but rather returns an empty string for the end of file immediately.
So then, for monitoring files, I have a few questions: Is there any way I can set this to block? I’m seeing empty strings in this loop, is there any chance they don’t actually represent an end of line? Do files that are still opened for writing have end of line characters added to them?
os.path.getsizeto see if there is a difference in the filef.readlineonly if there is a difference in sizeseekfirst to make sure you are actually reading the last line.f.readline()[0:-1]to get rid of the trailing\n(thanks to rm for pointing out thatrstripmight cause you problems)sleep(Trust me, your computer will thank you)