广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python实现tail -f命令功能
  • 600
分享到

python实现tail -f命令功能

命令功能python 2023-01-31 07:01:34 600人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

#!/usr/bin/env python #!encoding:utf-8 ''' Python-Tail - Unix tail follow implementation in Python. python-tail can be

#!/usr/bin/env python
#!encoding:utf-8
'''
Python-Tail - Unix tail follow implementation in Python.

python-tail can be used to monitor changes to a file.

Example:
    import tail

    # Create a tail instance
    t = tail.Tail('file-to-be-followed')

    # ReGISter a callback function to be called when a new line is found in the followed file.
    # If no callback function is registerd, new lines would be printed to standard out.
    t.register_callback(callback_function)

    # Follow the file with 5 seconds as sleep time between iterations.
    # If sleep time is not provided 1 second is used as the default time.
    t.follow(s=5) '''

# Author - Kasun Herath <kasunh01 at gmail.com>
# Source - https://GitHub.com/kasun/python-tail

import os
import sys
import time

class Tail(object):
    ''' Represents a tail command. '''
    def __init__(self, tailed_file):
        ''' Initiate a Tail instance.
            Check for file validity, assigns callback function to standard out.

            Arguments:
                tailed_file - File to be followed. '''
        self.check_file_validity(tailed_file)
        self.tailed_file = tailed_file
        self.callback = sys.stdout.write

    def follow(self, s=1):
        ''' Do a tail follow. If a callback function is registered it is called with every new line.
        Else printed to standard out.

        Arguments:
            s - Number of seconds to wait between each iteration; Defaults to 1. '''

        with open(self.tailed_file) as file_:
            # Go to the end of file
            file_.seek(0,2)
            while True:
                curr_position = file_.tell()
                line = file_.readline()
                if not line:
                    file_.seek(curr_position)
                else:
                    self.callback(line)
                time.sleep(s)

    def register_callback(self, func):
        ''' Overrides default callback function to provided function. '''
        self.callback = func

    def check_file_validity(self, file_):
        ''' Check whether the a given file exists, readable and is a file '''
        if not os.access(file_, os.F_OK):
            raise TailError("File '%s' does not exist" % (file_))
        if not os.access(file_, os.R_OK):
            raise TailError("File '%s' not readable" % (file_))
        if os.path.isdir(file_):
            raise TailError("File '%s' is a directory" % (file_))

class TailError(Exception):
    def __init__(self, msg):
        self.message = msg
    def __str__(self):
        return self.message
		
if __name__=='__main__':
	args = sys.argv
	if len(args)<2:
		print 'need one filename parameter'
		exit(1)
	t = Tail(args[1])
	#t.register_callback(print_line)
	t.follow()

--结束END--

本文标题: python实现tail -f命令功能

本文链接: https://www.lsjlt.com/news/191272.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python实现tail -f命令功能
    #!/usr/bin/env python #!encoding:utf-8 ''' Python-Tail - Unix tail follow implementation in Python. python-tail can be...
    99+
    2023-01-31
    命令 功能 python
  • Go语言中怎么实现tail命令的功能
    这篇文章主要讲解了“Go语言中怎么实现tail命令的功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Go语言中怎么实现tail命令的功能”吧!在 Go 语言中,我们可以使用 os 包和 b...
    99+
    2023-07-06
  • Python实现Linux命令xxd -i功能
    一. Linux xxd -i功能 Linux系统xxd命令使用二进制或十六进制格式显示文件内容。若未指定outfile参数,则将结果显示在终端屏幕上;否则输出到outfile中。详细的...
    99+
    2022-06-04
    命令 功能 Python
  • 使用Python实现touch命令功能
           公司指定的办公平台是Windows,而且给了诸多不自由的限制。如果对shell有一点点依赖,那么会是一个很麻烦的事情,毕竟对Windows的批处理命令熟悉度不够。由于touch命令使用的频繁,我觉得可以拿Python去模拟一...
    99+
    2023-01-31
    命令 功能 Python
  • Node.js中使用Log.io在浏览器中实时监控日志(等同tail -f命令)
    今天,抽空了浏览了下node.js ,哈哈,看了一篇入门的文章(http://www.nodebeginner.org/index-zh-cn.html),自我感觉是入门了,不过里面一句话,挺有感悟: 不...
    99+
    2022-06-04
    实时监控 器中 命令
  • Linux中如何实现tail命令的使用
    这篇文章跟大家分析一下“Linux中如何实现tail命令的使用”。内容详细易懂,对“Linux中如何实现tail命令的使用”感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着小编一起深入学习“Linux...
    99+
    2023-06-28
  • Rundll.exe 多功能实现命令
    首先,请你做个小实验(请事先保存好你正在执行的程式的结果)∶点击“开始-程式-Ms-DOS方式”,进入Dos视窗,然後键入“rundll32.exe user.exe,restartW...
    99+
    2023-05-24
    Rundll.exe 多功能 命令 Rundll
  • Rundll.exe 多功能实现命令的示例
    这篇文章主要介绍Rundll.exe 多功能实现命令的示例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先,请你做个小实验(请事先保存好你正在执行的程式的结果)∶点击“开始-程式-Ms-Dos方式”,进入Dos视窗...
    99+
    2023-06-14
  • Golang实现简易的命令行功能
    目录前言开始flag.Stringflag.Intflag.StringVarflag.IntVar定义命令行参数实现 -f -v 是否强制拷贝copyFileAction 实现co...
    99+
    2023-02-13
    Golang实现命令行功能 Golang命令行功能 Golang命令行
  • 怎么用vbs实现cmd多命令运行功能
    这篇文章主要讲解了“怎么用vbs实现cmd多命令运行功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用vbs实现cmd多命令运行功能”吧!on error resu...
    99+
    2023-06-09
  • python实现Linux命令wget
    #!/bin/python #coding:utf-8 def wget(url,new_name=""): ''' wget封装,需提供下载地址,新文件名参数可省略 ...
    99+
    2023-01-31
    命令 python Linux
  • golang实现命令行程序的使用帮助功能
    通过flag包我们可以很方便的实现命令行程序的参数标志,接下来我们来看看如何实现命令行程序的使用帮助,通常以参数标志-h或--help的形式来使用。 自动生成使用帮助 我们只需要声明...
    99+
    2022-11-13
  • shell命令行如何实现输入与输出功能
    这篇文章将为大家详细讲解有关shell命令行如何实现输入与输出功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。标准输入/输出和重定向,Linux发行版Fedora Core Linux,而Red Hat...
    99+
    2023-06-09
  • win10系统有哪些命令一键能实现进入功能页?
    Windows10系统的设置,我们可以使用命令一键进入设置的功能页,对于不太熟悉Windows10设置功能的朋友,使用起来就方便多了。下面介绍使用命令一键进入设置功能页的方法以及设置的命令。 1、右键点击系统桌面左下角的...
    99+
    2023-05-21
    win10 系统 功能
  • Python实战(1)模拟wc命令部分功
    模拟wc命令统计行和字符的功能。 # vim wc.py #!/usr/bin/env python '''         Author:diege         Email:diege@foxmail.com         Date...
    99+
    2023-01-31
    实战 命令 Python
  • linux中怎么利用ntp命令实现时间同步功能
    这篇文章给大家介绍linux中怎么利用ntp命令实现时间同步功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。如果服务器的时间出现混乱,将导致很多意想不到的问题。使用NTP,可以使服务器获取正确的时间,从而避免出现问题...
    99+
    2023-06-13
  • python实现FTP功能
    如果只是想下载文件,那么urllib2模块就可以轻松完成这个任务,而且比FTP更简单,但是FTP一些特殊功能urllib2模块不具备。(网络编程基础P277)   #!/usr/bin/python #-*- coding:UTF-8 -*...
    99+
    2023-01-31
    功能 python FTP
  • Python远程linux执行命令实现
    1、远程登录到linux上,使用到的模块paramiko #远程登陆操作系统 def ssh(sys_ip,username,password,cmds): try #创建ssh客户端 clien...
    99+
    2022-06-04
    Python远程linux执行命令 Python linux远程命令
  • Python实现subprocess执行外部命令
    目录一、Python执行外部命令1、subprocess模块简介2、subprocess模块的遍历函数3、subprocess模块的Popen类(PyCharm)4、使用python自动安i装并启动mongodb一、...
    99+
    2022-06-03
    Python 执行外部命令
  • 实现 Python 脚本生成命令行
    目录Fire使用方法方法支持类支持重新改写有时候我们会有这样的一个需求: 我们定义了一个 Python 的方法,方法接收一些参数,但是调用的时候想将这些参数用命令行暴露出来。 比如说...
    99+
    2022-11-11
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作