Python实现读取文件最后n行的方法
作者:design321 时间:2023-08-02 10:33:32
本文实例讲述了Python实现读取文件最后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
#获取最后一行的结果
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 '数据条数正常'
print 'OK'
else:
print '数据太少,检查发邮件'
以上处理和日志文件格式为
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
读取最后一行:
#返回文件最后一行函数
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
希望本文所述对大家Python程序设计有所帮助。
标签:Python,读取,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
从Vista地址栏到网站导航菜单
2009-09-08 12:35:00
![](https://img.aspxhome.com/file/UploadPic/20099/8/nav-62s.jpg)
php实现HTML实体编号与非ASCII字符串相互转换类实例
2023-11-15 06:12:14
![](https://img.aspxhome.com/file/2023/5/55545_0s.png)
asp如何处理页面执行时发生的错误?
2009-11-14 20:43:00
用python生成一张壁纸实例代码
2022-06-06 10:26:51
![](https://img.aspxhome.com/file/2023/7/127707_0s.png)
详解JavaScript实现JS弹窗的三种方式
2024-04-19 10:42:20
![](https://img.aspxhome.com/file/2023/5/135815_0s.png)
python中的字符串占位符的"{0:2}"
2021-04-28 20:23:39
python翻译软件实现代码(使用google api完成)
2023-05-19 17:25:43
MySQL server has gone away 问题的解决方法
2024-01-18 11:46:11
python关键字and和or用法实例
2023-12-19 20:07:40
Python linecache.getline()读取文件中特定一行的脚本
2023-03-09 13:18:05
![](https://img.aspxhome.com/file/2023/5/67725_0s.gif)
python数字图像处理之高级滤波代码详解
2022-06-30 15:02:09
![](https://img.aspxhome.com/file/2023/6/133406_0s.jpg)
Python3 shelve对象持久存储原理详解
2022-06-30 13:43:39
![](https://img.aspxhome.com/file/2023/2/86542_0s.png)
XHTML下,JS浮动代码失效的问题
2024-05-28 15:37:51
MySQL存储过程savepoint rollback to
2008-12-03 16:02:00
基于模板引擎Jade的应用(详解)
2024-05-11 10:14:57
![](https://img.aspxhome.com/file/2023/6/125736_0s.jpg)
Python udp网络程序实现发送、接收数据功能示例
2023-12-03 11:12:52
![](https://img.aspxhome.com/file/2023/9/108109_0s.png)
抛砖:如何进行互联网项目开发
2010-01-25 12:25:00
![](https://img.aspxhome.com/file/UploadPic/20101/25/internet-poject01-87s.jpg)
使用python scrapy爬取天气并导出csv文件
2023-02-25 16:16:10
![](https://img.aspxhome.com/file/2023/8/90758_0s.png)
浅谈numpy 函数里面的axis参数的含义
2023-06-04 11:23:35
Python中turtle库的使用实例
2023-08-01 23:05:56
![](https://img.aspxhome.com/file/2023/1/62021_0s.png)