如何改进这个类似尾巴的Python代码
我只是想知道你们是否有比我想出的更好的方法。我想要的是制作一个类似“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:]
I just want to know if you guys have better ways of doing this than the one I came up with. What I want is to make a "tail -f"-like script, but one that will look actively for a string and print only the text related to that string, in real time. As you can see from the code I'm looking for MAC addresses, but I guess it could be used for some other purposes.
I was thinking that there must be a better way of doing this. Maybe one of you guys knows a clever algorithm or a command that does this better. Thanks for your help
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:]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是将此作为 Python 练习还是可以使用 shell?
你能简单地将尾部输出传送到 grep 中吗?
您可以将其放入脚本中并创建文件和模式参数。
使用 grep,您还可以使用 -A 和 -B 开关将上下文添加到输出中。
Are you doing this as a Python exercise or can you use the shell?
Can you simply pipe the tail output into a grep?
You could put this into a script and make the file and pattern args.
Using grep you can also add context to your output by using the -A and -B switches.