我对构建一个python脚本很感兴趣,它可以提供每个间隔(可能是分钟)写入文件的行数的统计信息。我有一些文件,当数据进入时被写入,每个用户都有一个新行,通过外部程序传递数据。知道每个x有多少行给了我一个度量标准,可以用于将来的扩展规划。输出文件由行组成,所有行的长度都相对相同,并且在末尾都有行返回。我在想写一个脚本,它可以这样做:在一个特定的点测量文件的长度,然后在将来的另一个点测量它,减去这两个,得到我的结果。。。但是我不知道这是否理想,因为它需要时间来测量文件的长度,这可能会扭曲我的结果。有人有其他想法吗?在
在
根据人们所说的,我把这个放在一起开始:import os
import subprocess
import time
from daemon import runner
#import daemon
inputfilename="/home/data/testdata.txt"
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/count.pid'
self.pidfile_timeout = 5
def run(self):
while True:
count = 0
FILEIN = open(inputfilename, 'rb')
while 1:
buffer = FILEIN.read(8192*1024)
if not buffer: break
count += buffer.count('\n')
FILEIN.close( )
print count
# set the sleep time for repeated action here:
time.sleep(60)
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
它的工作是每60秒获取一次计数并将其打印到屏幕上,我的下一步是我猜的数学。在
再编辑一次:我以一分钟的间隔添加了计数的输出:
^{pr2}$