最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)定時提取實時日志程序

 更新時間:2018年06月22日 11:41:09   作者:墨飏韶年  
這篇文章主要為大家詳細介紹了python實現(xiàn)定時提取實時日志程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

這是一個定時讀取 實時日志文件的程序。目標(biāo)文件是target_file. 它是應(yīng)用程序?qū)崟r寫入的。

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

#!/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 '當(dāng)前讀到了', 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: 
  #如果總行數(shù)小于當(dāng)前行,那么認為文件更新了,從第一行開始讀。 
  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) 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

北海市| 天全县| 平定县| 新余市| 六枝特区| 阿瓦提县| 苗栗市| 哈巴河县| 阜新| 南宁市| 翼城县| 林州市| 军事| 青州市| 大足县| 安国市| 长宁区| 许昌市| 云南省| 资阳市| 大荔县| 隆化县| 军事| 新竹市| 开原市| 上饶县| 肇州县| 汾阳市| 弥渡县| 年辖:市辖区| 迁西县| 内丘县| 深泽县| 琼结县| 海林市| 盐亭县| 浦城县| 凉山| 来安县| 岗巴县| 阿拉善左旗|