大家讲道理2017-04-18 09:27:24
几种方式:
用subprocess调tail -f
用pyinotiy监听文件变化
自己实现,比如先拿到文件大小,读到最后一个字节,然后往回退到第一个n,这就是倒数第一行了,然后依次往回退
怪我咯2017-04-18 09:27:24
楼上几位说的都可行, 不过 @nealnote 的方法如果读大文件会有性能问题, 建议用 @manong 所说, subprocess
调unix
系统自带的一个tail
程序去做, 具体实现如下:
import subprocess
fh = subprocess.Popen("tail -n 50 /var/log/dmesg", stdout=subprocess.PIPE, shell=True)
for line in fh.stdout.readlines():
print(line.decode('ascii'), end="")
运行结果:
...
...
rdac: device handler registered
device-mapper: multipath round-robin: version 1.0.0 loaded
EXT4-fs (sdd5): mounted filesystem with ordered data mode. Opts:
EXT4-fs (sdd2): mounted filesystem with ordered data mode. Opts:
EXT4-fs (dm-1): mounted filesystem with ordered data mode. Opts:
EXT4-fs (dm-0): warning: maximal mount count reached, running e2fsck is recommended
EXT4-fs (dm-0): mounted filesystem with ordered data mode. Opts:
Adding 1023996k swap on /dev/sdd3. Priority:-1 extents:1 across:1023996k