国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現定時提取實時日志程序

2020-02-15 21:58:33
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python定時提取實時日志的具體代碼,供大家參考,具體內容如下

這是一個定時讀取 實時日志文件的程序。目標文件是target_file. 它是應用程序實時寫入的。

我要做的是,每個5秒鐘,提取一次該日志文件中的內容,然后生成另一個文件,最后把這些文件都匯總。

#!/usr/local/bin/python # coding:utf-8  import fileinput import time import os  target_file = 'user.log' init_flag = True # 初次加載程序 time_kick = 5  record_count = 0  while True:  print '當前讀到了', record_count  #沒有日志文件,等待  if not os.path.exists(target_file):  print 'target_file not exist'  time.sleep(time_kick)  continue   try:  ip = '10.10.1.100'  easytime = time.strftime('%Y%m%d_%H%M%S', time.localtime())  file_name = '%s_user_%s.log' % (ip,easytime)  f_w = open(file_name, 'w')  if init_flag:   #讀取整個文件   for eachline in fileinput.input(target_file):   print eachline   f_w.write(eachline)   record_count += 1    init_flag = False  else:   #如果總行數小于當前行,那么認為文件更新了,從第一行開始讀。   total_count = os.popen('wc -l %s' % target_file).read().split()[0]   total_count = int(total_count)   if total_count < record_count:   record_count = 0    for eachline in fileinput.input(target_file):   line_no = fileinput.filelineno()   if line_no > record_count:    print eachline    f_w.write(eachline)    record_count += 1   f_w.close()  except:  pass  time.sleep(time_kick) 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安宁市| 民勤县| 都昌县| 恩施市| 古丈县| 东宁县| 枣阳市| 达州市| 肥西县| 庆云县| 宁蒗| 嘉义县| 诸暨市| 沽源县| 长治市| 雷波县| 蒲江县| 兴安县| 凌源市| 龙里县| 巴林右旗| 离岛区| 沾化县| 浠水县| 兰州市| 苏州市| 景谷| 广河县| 太仆寺旗| 安国市| 额尔古纳市| 蕲春县| 且末县| 正蓝旗| 子长县| 玉山县| 边坝县| 昭苏县| 平昌县| 南丰县| 平和县|