[Tutor] Reset while loop
David
david at abbottdavid.com
Wed May 6 04:45:10 CEST 2009
Hi, not even sure if I can ask this very smartly but we shall try:
I was playing around with trying to create a python program like tail -f
as discussed on the list. i came up with this;
def tail_it(bufsize=8192, linesep=__import__('os').linesep):
f = None
if f == None:
f = open(tail_fname, 'rb')
f.seek(0, 2)
pos, tailsize = divmod(f.tell(), bufsize)
if tailsize == 0:
pos = max(0, pos-1)
pos *= bufsize
f.seek(pos)
lines = f.read().split(linesep)
x = len(lines)
x = x-2
print lines[x:]
f.close()
while True:
new_time = os.stat(tail_fname).st_mtime
if new_time > old_time:
f = open(tail_fname, 'rb')
f.seek(0, 2)
pos, tailsize = divmod(f.tell(), bufsize)
if tailsize == 0:
pos = max(0, pos-1)
pos *= bufsize
f.seek(pos)
lines = f.read().split(linesep)
x = len(lines)
x = x-2
print lines[x:]
time.sleep(sec_to_wait)
f.close()
[output]
['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed
for user root', '']
['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed
for user root', '']
['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed
for user root', '']
['May 5 22:40:01 opteron cron[22996]: (root) CMD (test -x
/usr/sbin/run-crons && /usr/sbin/run-crons )', '']
['May 5 22:40:01 opteron cron[22996]: (root) CMD (test -x
/usr/sbin/run-crons && /usr/sbin/run-crons )', '']
['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change
4001', '']
['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change
4001', '']
['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change
4001', '']
['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change
4001', '']
[/output]
That was with sec_to_wait set at 30
The problem I am having is it will print something whatever sec_to_wait
is set to even if the file's st_mtime is the same. I want it to only
prine when the st_mtime has changed. I think I need to update the while
loop and make old_time = new-time and start over but I need some
direction to get started, thanks
-david
--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
More information about the Tutor
mailing list