Python监控文件内容变化

利用seek监控文件内容,并打印出变化内容:

#/usr/bin/env python
#-*- coding=utf-8 -*-

pos = 0
while True:
    con = open("a.txt")
    if pos != 0:
        con.seek(pos,0)
    while True:
	line = con.readline()
	if line.strip():
	    print line.strip()
	pos = pos + len(line)
	if not line.strip():
	    break
    con.close()

利用工具pyinotify监控文件内容变化,当文件逐渐变大时,可轻松完成任务:

#!/usr/bin/env python
#-*- coding=utf-8 -*-
import os
import datetime
import pyinotify
import logging

pos = 0
def printlog():
    global pos
    try:
        fd = open("log/a.txt")
	if pos != 0:
	    fd.seek(pos,0)
	while True:
	    line = fd.readline()
	    if line.strip():
	        print line.strip()
	    pos = pos + len(line)
	    if not line.strip():
		break
	fd.close()
    except Exception,e:
	print str(e)

class MyEventHandler(pyinotify.ProcessEvent):
    def process_IN_MODIFY(self,event):
        try:
	    printlog()
	except Exception,e:
	    print str(e)

def main():
    printlog()
    wm = pyinotify.WatchManager()
    wm.add_watch("log/a.txt",pyinotify.ALL_EVENTS,rec=True)
    eh = MyEventHandler()
    notifier = pyinotify.Notifier(wm,eh)
    notifier.loop()
if __name__ == "__main__":
    main()

原文地址:http://blog.51cto.com/guoshiwei/2124306

时间: 2024-08-01 11:16:12

Python监控文件内容变化的相关文章

python 修改文件内容

python 修改文件内容 一.修改原文件方式 1 def alter(file,old_str,new_str): 2 """ 3 替换文件中的字符串 4 :param file:文件名 5 :param old_str:就字符串 6 :param new_str:新字符串 7 :return: 8 """ 9 file_data = "" 10 with open(file, "r", encoding

Java Web应用中自动实时检测资源文件内容变化

在Java Web应用中,我们经常需要配置文件来定制系统行为,这些配置文件可能包括:类路径下的文件和文件夹.非类路径下的绝对路径和相对路径的文件和文件夹,在分布式环境中,还需要通过HTTP从统一集中的Web服务器中获得配置信息,如何对这些配置信息进行自动加载并实时检测变化呢? Java分布式中文分词组件 - word分词已经实现了这个功能,我们看看是如何实现的: package org.apdplat.word.util; import java.io.BufferedReader; impor

Storm监控文件夹变化 统计文件单词数量

监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

python改动文件内容,不须要read,write多个动作。

python  要改动文件内容,经常使用 是先read.后write , 再 rename.非常不爽. 比方:须要 把       yuv_dir ="../HD/"   # "H:/HD_Master/1080i25/" 改为       yuv_dir ="C:/HD/"   # "H:/HD_Master/1080i25/" 非常easy,但实际不好操作,由于read后文件指针就到后一行了.要使用seek到前一行等,非常

python修改文件内容,不需要read,write多个动作。

python  要修改文件内容,常用 是先read,后write , 再 rename,很不爽. 比如:需要 把       yuv_dir ="../HD/"   # "H:/HD_Master/1080i25/" 改为       yuv_dir ="C:/HD/"   # "H:/HD_Master/1080i25/" 很简单,但实际不好操作,因为read后文件指针就到后一行了,要使用seek到前一行等,很不好. 很多应

利用pyinotify监控文件内容,像tailf命令但比它更强

Linux的tail/tailf命令使用了内核提供的inotify功能,下面的Python例子也使用inotify实现比tail/tailf更强的监控文件功能. watchfile.py #!/usr/bin/python import sys, os, pyinotify notifier = None monfile = None lastsize = 0 wm = None wd = 0 def roll_file(filename): global lastsize fd = os.op

Python修改文件内容

import sys import os name=(sys.argv) #用来存储输入的4个参数 if len(name)<4: #判断输入参数是否小于4个 print('至少传入4个参数') exit() #如果小于4个参数,不往下执行直接退出 f=open('d:/'+name[1],'a+') #以追加方式打开要更改的文件 f1=open('d:/'+name[1]+'.new','w') #同时以写的方式打开一个新文件 f.seek(0) #文件指针指向开头 for line in f

学习python对文件内容的操作

在Python中对文件的操作使用open(filename,"w"),这里filename是文件名称,w指写入操作,会自动覆盖文件中的所有内容,还有r读操作和a追加操作等. 需要注意的是:只能同时进行一项操作,比如写的时候不能读,即使你使用w+参数,也只是读出来空白内容,不过不会报错,所有执行完一项操作使用新的参数才能继续不同的操作,如: f = open(filename,'w')  #以写操作打开文件filename,文件存在则覆盖,不存在则建立 f.write('this is 

Python替换文件内容(文件处理)

#需求 用Python来替换掉文件里面的内容 #脚本内容如下 #!/usr/bin/env python #_*_ coding:utf-8 _*_ import sys,os #小于四个位置变量则退出脚本 if len(sys.argv) <4:     print "useage: ./file_replace.py old_text new_text filename"     sys.exit()      #设置三个位置变量 old_text,new_text = sy