python 实时遍历日志文件


Posted in Python onApril 12, 2016

open 遍历一个大日志文件

使用 readlines() 还是 readline() ?

总体上 readlines() 不慢于python 一次次调用 readline(),因为前者的循环在C语言层面,而使用readline() 的循环是在Python语言层面。

但是 readlines() 会一次性把全部数据读到内存中,内存占用率会过高,readline() 每次只读一行,对于读取 大文件, 需要做出取舍。

如果不需要使用 seek() 定位偏移, for line in open('file') 速度更佳。

使用 readlines(),适合量级较小的日志文件

import os
import time
def check():
p = 
while True:
f = open("log.txt", "r+")
f = open("result.txt", "a+")
f.seek(p, )
#readlines()方法
filelist = f.readlines()
if filelist:
for line in filelist:
#对行内容进行操作
f.write(line)
#获取当前位置,为下次while循环做偏移
p = f.tell()
print 'now p ', p
f.close()
f.close()
time.sleep()
if __name__ == '__main__':
check()

使用 readline(),避免内存占用率过大

import os
import time
def check():
p = 
while True:
f = open("log.txt", "r+")
f = open("result.txt", "a+")
f.seek(p, )
#while readline()方法
while True:
l = f.readline()
#空行同样为真
if l:
#对行内容操作
f.write(l)
else:
#获取当前位置,作为偏移值
p = f.tell()
f.close()
f.close()
break
print 'now p', p
time.sleep()
if __name__ == '__main__':
check()
Python 相关文章推荐
Python version 2.7 required, which was not found in the registry
Aug 26 Python
Python中不同进制的语法及转换方法分析
Jul 27 Python
Python中矩阵库Numpy基本操作详解
Nov 21 Python
Python使用flask框架操作sqlite3的两种方式
Jan 31 Python
python距离测量的方法
Mar 06 Python
Python实现拷贝/删除文件夹的方法详解
Aug 29 Python
对Python中小整数对象池和大整数对象池的使用详解
Jul 09 Python
Django ORM 常用字段与不常用字段汇总
Aug 09 Python
Python切图九宫格的实现方法
Oct 10 Python
python selenium 执行完毕关闭chromedriver进程示例
Nov 15 Python
利用python制作拼图小游戏的全过程
Dec 04 Python
Django+Nginx+uWSGI 定时任务的实现方法
Jan 22 Python
python字符串连接方法分析
Apr 12 #Python
python去除文件中空格、Tab及回车的方法
Apr 12 #Python
Python脚本实现虾米网签到功能
Apr 12 #Python
Python脚本简单实现打开默认浏览器登录人人和打开QQ的方法
Apr 12 #Python
用Python写冒泡排序代码
Apr 12 #Python
详解Python的Django框架中manage命令的使用与扩展
Apr 11 #Python
对Python的Django框架中的项目进行单元测试的方法
Apr 11 #Python
You might like
PHP开发大型项目的一点经验
2006/10/09 PHP
php数据入库前清理 注意php intval与mysql的int取值范围不同
2010/12/12 PHP
php生成局部唯一识别码LUID的代码
2012/10/06 PHP
php中file_get_content 和curl以及fopen 效率分析
2014/09/19 PHP
php curl 获取https请求的2种方法
2015/04/27 PHP
yii2.0数据库迁移教程【多个数据库同时同步数据】
2016/10/08 PHP
Laravel 模型使用软删除-左连接查询-表起别名示例
2019/10/24 PHP
laravel 查询数据库获取结果实现判断是否为空
2019/10/24 PHP
jQuery1.5.1 animate方法源码阅读
2011/04/05 Javascript
JavaScript的instanceof运算符学习教程
2016/06/08 Javascript
JS实现字符串转驼峰格式的方法
2016/12/16 Javascript
基于jQuery ztree实现表格风格的树状结构
2018/08/31 jQuery
利用原生JavaScript实现造日历轮子实例代码
2019/05/08 Javascript
javascript实现移动端红包雨页面
2020/06/23 Javascript
跟老齐学Python之使用Python操作数据库(1)
2014/11/25 Python
python实现telnet客户端的方法
2015/04/15 Python
分析Python中设计模式之Decorator装饰器模式的要点
2016/03/02 Python
python和shell获取文本内容的方法
2018/06/05 Python
判断python字典中key是否存在的两种方法
2018/08/10 Python
Python使用sklearn实现的各种回归算法示例
2019/07/04 Python
python编写微信公众号首图思路详解
2019/12/13 Python
pymysql的简单封装代码实例
2020/01/08 Python
python进度条显示之tqmd模块
2020/08/22 Python
CSS3弹性盒模型开发笔记(一)
2016/04/26 HTML / CSS
CSS3 新增选择器的实例
2019/11/13 HTML / CSS
法国床上用品商店:La Compagnie du lit
2019/12/26 全球购物
人力资源专业推荐信
2013/11/29 职场文书
毕业生个人的求职信范文
2013/12/03 职场文书
师范毕业生自我鉴定
2014/01/15 职场文书
个人求职信范文分享
2014/01/31 职场文书
高校优秀辅导员事迹材料
2014/05/07 职场文书
签约仪式策划方案
2014/06/02 职场文书
大学生国庆节65周年演讲稿范文
2014/09/25 职场文书
社区活动总结
2015/02/04 职场文书
期中考试后的感想
2015/08/07 职场文书
创业计划书之婴幼儿游泳馆
2019/09/11 职场文书