在服务器中包含一个大的日志文件。我想实现最近5天的日志数据。这是日志文件的一部分
Sat Jun 2 03:32:13 2012 [pid 12461] CONNECT: Client "66.249.68.236"
Sat Jun 2 03:32:13 2012 [pid 12460] [ftp] OK LOGIN: Client "66.249.68.236", anon password "gxxglxxxxt@google.com"
Sat Jun 2 03:32:14 2012 [pid 12462] [ftp] OK DOWNLOAD: Client "66.249.68.236", "/pub/10.5524/100001_101000/100022/readme.txt", 451 bytes, 1.39Kbyte/sec
Sat Jun 2 03:32:22 2012 [pid 12677] CONNECT: Client "66.249.68.236"
Sat Jun 2 03:32:23 2012 [pid 12676] [ftp] OK LOGIN: Client "66.249.68.236", anon password "xxxxxbot@google.com"
我使用该代码,但在打开(文件名)中有一些语法错误。命令是 python ex.py vsftp.log 有人可以帮我解决它。并创建一个新的日志文件来保存输出行。谢谢
from sys import argv
import time
script, filename = argv
with open(filename) as f:
lines= [line for line in f]
def OnlyRecent(line):
return time.strptime(line.split("[")[0].strip(),"%a %b %d %H:%M:%S %Y") <(time.time()-(60*60*24*5))
print "\n".join(filter(OnlyRecent,lines))
但是当我使用代码时,它可以工作吗?为什么从 sys 导入 argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
当我创建 ex.py 文件时
f =open('/opt/CLiMB/Storage1/log/vsftp.log','r')
print f.readline
键入 python ex.py
它在 0x 处显示文件对象的内置方法 readline ... 如何解决它。谢谢