从日志文件中读取,因为它是使用python编写的 [英] Read from a log file as it's being written using python

查看:121
本文介绍了从日志文件中读取,因为它是使用python编写的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个很好的方法来使用python实时读取日志文件。我想在写一个日志文件时一次处理行。不知何故,我需要继续尝试读取文件,直到创建它,然后继续处理线,直到我终止进程。有没有适当的方法来做到这一点?谢谢。

I'm trying to find a nice way to read a log file in real time using python. I'd like to process lines from a log file one at a time as it is written. Somehow I need to keep trying to read the file until it is created and then continue to process lines until I terminate the process. Is there an appropriate way to do this? Thanks.

推荐答案

你可以尝试这样的东西:

You could try with something like this:

import time

while 1:
    where = file.tell()
    line = file.readline()
    if not line:
        time.sleep(1)
        file.seek(where)
    else:
        print line, # already has newline

here

这篇关于从日志文件中读取,因为它是使用python编写的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆