国产探花免费观看_亚洲丰满少妇自慰呻吟_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) 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 利辛县| 乌鲁木齐县| 肥西县| 延津县| 巫溪县| 邻水| 八宿县| 余庆县| 满洲里市| 太康县| 南乐县| 海丰县| 甘孜县| 洮南市| 泰安市| 阳朔县| 襄垣县| 黎城县| 广水市| 拉萨市| 昌江| 调兵山市| 安义县| 元氏县| 马山县| 汽车| 龙门县| 海安县| 通江县| 九江市| 永平县| 徐闻县| 镇坪县| 车险| 即墨市| 凌云县| 镇宁| 都匀市| 新巴尔虎右旗| 石河子市| 赤水市|