Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何改进这种类似尾部的Python代码_Python_Optimization_Tail - Fatal编程技术网

如何改进这种类似尾部的Python代码

如何改进这种类似尾部的Python代码,python,optimization,tail,Python,Optimization,Tail,我只是想知道你们有没有比我想出的更好的方法。我想要的是制作一个类似于“tail-f”的脚本,但它将主动查找字符串并实时打印与该字符串相关的文本。正如您从代码中看到的,我正在寻找MAC地址,但我想它可以用于其他目的 我在想,一定有更好的办法。也许你们当中有人知道一个聪明的算法或者一个更好的命令。谢谢你的帮助 import time, os, sys from datetime import date # Function to get the file size, it will help us

我只是想知道你们有没有比我想出的更好的方法。我想要的是制作一个类似于“tail-f”的脚本,但它将主动查找字符串并实时打印与该字符串相关的文本。正如您从代码中看到的,我正在寻找MAC地址,但我想它可以用于其他目的

我在想,一定有更好的办法。也许你们当中有人知道一个聪明的算法或者一个更好的命令。谢谢你的帮助

import time, os, sys
from datetime import date

# Function to get the file size, it will help us go to the end of a file
def current_file_size(filename):
    file_results = os.stat(filename)
    file_size = file_results[6]
    return file_size

# Check for correct usage
if len(sys.argv) != 2:
    print "Usage: %s <mac_address>" % sys.argv[0]
    sys.exit()

#Get the date in the format that the log uses
now = date.today()
todays_date = now.strftime("%Y%m%d")

#Set the filename and open the file
filename = 'complete.log'
file = open(filename,'r')

#Find the size of the file and move to the end
st_size = current_file_size(filename)
file.seek(st_size)

while 1:
    where = file.tell()   # current position of the file
    time.sleep(2)         # sleep for a little while
    st_size = current_file_size(filename)
    if st_size > where:       # if there's new text
        alotoflines = file.read(st_size-where)    # get the new lines as a group
        # search for the tag+mac address
        found_string = alotoflines.find("<mac v=\"" + sys.argv[1])
        if found_string > 0:
            # search for the immediately prior date instance from where the MAC address
            # is. I know that the log entry starts there
            found_date_tag = alotoflines.rfind(todays_date,0,found_string)
            print alotoflines[found_date_tag:]
导入时间、操作系统、系统 起始日期时间导入日期 #函数获取文件大小,它将帮助我们转到文件的末尾 定义当前文件大小(文件名): file_results=os.stat(文件名) 文件大小=文件结果[6] 返回文件大小 #检查用法是否正确 如果len(sys.argv)!=2: 打印“用法:%s”%sys.argv[0] sys.exit() #以日志使用的格式获取日期 现在=日期。今天() todays\u date=now.strftime(“%Y%m%d”) #设置文件名并打开文件 文件名='complete.log' 文件=打开(文件名'r') #找到文件的大小并移到末尾 st_size=当前文件大小(文件名) 查找文件(st_大小) 而1: 其中=file.tell()#文件的当前位置 时间到了。睡觉(2)#睡一会儿 st_size=当前文件大小(文件名) 如果st_size>where:#如果有新文本 alotoflines=file.read(st#U size-where)#将新行作为一个组获取 #搜索标记+mac地址 已找到\u string=alotoflines.find(“0: #搜索MAC地址所在的前一日期实例 #是的。我知道日志条目从那里开始 found\u date\u tag=alotoflines.rfind(todays\u date,0,found\u string) 打印一行[找到日期标记:]
您这样做是作为Python练习,还是可以使用shell

您可以简单地将尾部输出导入grep吗

tail -F myfile.txt | egrep --line-buffered myPattern
您可以将其放入脚本中,并将文件和模式参数设置为args


使用grep,您还可以使用-A和-B开关将上下文添加到输出中。

您这样做是作为Python练习还是可以使用shell

您可以简单地将尾部输出导入grep吗

tail -F myfile.txt | egrep --line-buffered myPattern
您可以将其放入脚本中,并将文件和模式参数设置为args


使用grep,您还可以使用-A和-B开关将上下文添加到输出中。

更改
if st_size>where:
变量
where
,因为它实际上是python内置的。此外,您还可以将文档字符串添加到函数中,而不是添加注释。记住,文档字符串解释了它的作用,注释解释了它是如何实现的。您有没有查看此处提出的解决方案?肖恩所链接的问题是做一些与“tail”相当的事情。不过,你所要求的是“tail-f”,这是一种完全不同的行为。你可能想查看Tzury Bar Yochay关于这个问题的答案,我发现它非常有效:如果st_size>where:变量
where
,因为它实际上是python内置的。你也可以向函数中添加文档字符串,而不是注释。记住,文档字符串解释它的作用,注释解释如何。你检查过这里提出的解决方案吗?Sean链接到的问题是做一些与“tail”等效的事情。你是什么你可能想看看Tzury Bar Yochay关于这个问题的答案,我发现这个答案很有效:我不知道在模式匹配之前或之后要输出多少行,所以我不知道在-A和-B开关上放置什么我不知道如何放置在模式匹配之前或之后要输出许多行,因此我不知道在-A和-B开关上放置什么