Python實(shí)現(xiàn)讀取文件最后n行的方法
本文實(shí)例講述了Python實(shí)現(xiàn)讀取文件最后n行的方法。分享給大家供大家參考,具體如下:
# -*- coding:utf8-*- import os import time import datetime import math import string def get_last_line(inputfile) : filesize = os.path.getsize(inputfile) blocksize = 1024 dat_file = open(inputfile, 'r') last_line = "" lines = dat_file.readlines() count = len(lines) if count>60: num=60 else: num=count i=1; lastre = [] for i in range(1,(num+1)): if lines : n = -i last_line = lines[n].strip() #print "last line : ", last_line dat_file.close() #print i lastre.append(last_line) return lastre #獲取最后一行的結(jié)果 re = get_last_line('../update/log/rtime/rtime20130805.log') print len(re) for n in re: strlist = n.split(' ') if strlist[1] == 'ok' and string.atoi(strlist[2])>1000: print '數(shù)據(jù)條數(shù)正常' print 'OK' else: print '數(shù)據(jù)太少,檢查發(fā)郵件'
以上處理和日志文件格式為
2013-08-05 16:09:30 ok 1673 2013-08-05 16:10:34 ok 1628 2013-08-05 16:11:55 ok 71 2013-08-05 16:13:02 ok 1441 2013-08-05 16:14:06 ok 1634 2013-08-05 16:15:10 ok 1717 2013-08-05 16:16:14 ok 1687 2013-08-05 16:17:18 ok 1642 2013-08-05 16:18:27 ok 1655 2013-08-05 16:19:33 ok 1655
讀取最后一行:
#返回文件最后一行函數(shù) def get_last_line(inputfile) : filesize = os.path.getsize(inputfile) blocksize = 1024 dat_file = open(inputfile, 'r') last_line = "" if filesize > blocksize : maxseekpoint = (filesize // blocksize) dat_file.seek((maxseekpoint-1)*blocksize) elif filesize : #maxseekpoint = blocksize % filesize dat_file.seek(0, 0) lines = dat_file.readlines() if lines : last_line = lines[-1].strip() #print "last line : ", last_line dat_file.close() return last_line
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python URL操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python逐行讀取文件內(nèi)容的三種方法
- Python按行讀取文件的簡(jiǎn)單實(shí)現(xiàn)方法
- Python3讀取文件常用方法實(shí)例分析
- Python linecache.getline()讀取文件中特定一行的腳本
- Python用list或dict字段模式讀取文件的方法
- 解決python中os.listdir()函數(shù)讀取文件夾下文件的亂序和排序問(wèn)題
- 分析Python讀取文件時(shí)的路徑問(wèn)題
- Python讀取文件內(nèi)容的三種常用方式及效率比較
- python2.7讀取文件夾下所有文件名稱(chēng)及內(nèi)容的方法
- python多線(xiàn)程分塊讀取文件
相關(guān)文章
python自動(dòng)化測(cè)試selenium定位frame及iframe示例
這篇文章主要為大家介紹了python自動(dòng)化測(cè)試selenium定位frame及iframe示例的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11關(guān)于Python中 循環(huán)器 itertools的介紹
循環(huán)器是對(duì)象的容器,包含有多個(gè)對(duì)象。通過(guò)調(diào)用循環(huán)器的next()方法 (__next__()方法,在Python 3.x中),循環(huán)器將依次返回一個(gè)對(duì)象。直到所有的對(duì)象遍歷窮盡,循環(huán)器將舉出StopIteration錯(cuò)誤。這篇文章將對(duì)此做一個(gè)詳細(xì)介紹,感興趣的小伙伴請(qǐng)參考下面文字內(nèi)容2021-09-09Python實(shí)現(xiàn)按特定格式對(duì)文件進(jìn)行讀寫(xiě)的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)按特定格式對(duì)文件進(jìn)行讀寫(xiě)的方法,可實(shí)現(xiàn)文件按原有格式讀取與寫(xiě)入的功能,涉及文件的讀取、遍歷、轉(zhuǎn)換、寫(xiě)入等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11如何使用 python查詢(xún)Amazon DynamoDB
本文介紹了如何使用Python Boto3在Amazon DynamoDB上查詢(xún)DynamoDB 表、創(chuàng)建、列出和執(zhí)行其他 CRUD 活動(dòng)以及執(zhí)行其他維護(hù)任務(wù),本文給大家介紹的非常詳細(xì),需要的朋友參考下2023-06-06