3

私はPythonを使用して次のような大きなログファイルを開きます

Thu Oct  4 23:14:40 2012 [pid 16901] CONNECT: Client "66.249.74.228"
Thu Oct  4 23:14:40 2012 [pid 16900] [ftp] OK LOGIN: Client "66.249.74.228", anon     password "googlebot@google.com"
Thu Oct  4 23:17:42 2012 [pid 16902] [ftp] FAIL DOWNLOAD: Client "66.249.74.228",   "/pub/10.5524/100001_101000/100039/Assembly-2011/Pa9a_assembly_config4.scafSeq.gz",  14811136 bytes, 79.99Kbyte/sec
Fri Oct  5 00:04:13 2012 [pid 25809] CONNECT: Client "66.249.74.228"
Fri Oct  5 00:04:14 2012 [pid 25808] [ftp] OK LOGIN: Client "66.249.74.228", anon password "googlebot@google.com"
Fri Oct  5 00:07:16 2012 [pid 25810] [ftp] FAIL DOWNLOAD: Client "66.249.74.228", "/pub/10.5524/100001_101000/100027/Raw_data/PHOlcpDABDWABPE/090715_I80_FC427DJAAXX_L8_PHOlcpDABDWABPE_1.fq.gz", 14811136 bytes, 79.99Kbyte/sec
Fri Oct  5 00:13:19 2012 [pid 27354] CONNECT: Client "1.202.186.53"
Fri Oct  5 00:13:19 2012 [pid 27353] [ftp] OK LOGIN: Client "1.202.186.53", anon password "mozilla@example.com"

最近の7日間のレコードを取得するために、tailコマンドのようにファイルの終わりから行を読み取りたいです。

これが私のコードです。どうすれば変更できますか。

import time
f= open("/opt/CLiMB/Storage1/log/vsftp.log")
def OnlyRecent(line):
   if  time.strptime(line.split("[")[0].strip(),"%a %b %d %H:%M:%S %Y")>     time.gmtime(time.time()-(60*60*24*7)): 
    return True
return False
filename= time.strftime('%Y%m%d')+'.log'
f1= open(filename,'w')
for line in f:
 if OnlyRecent(line):
        print line
        f1.write(line)
f.close()
f1.close()
4

3 に答える 3

3

file.seek()を使用して、ファイルの末尾からのオフセットにジャンプします。たとえば、ファイルの先頭を読み取らずにファイルの最後の 1Kb を印刷するには、次のようにします。

with open("/opt/CLiMB/Storage1/log/vsftp.log") as f:
     f.seek(-1000, os.SEEK_END)
     print f.read()
于 2012-12-03T08:18:45.147 に答える
0

私はこれをチェックしませんでした。コードを再フォーマットするだけです:

  1. time モジュールからのあまり冗長でないインポート
  2. dropwhile代わりにfor..if
  3. withファイルを開閉するコンテキスト
  4. PEP8
  5. その他

-

from time import time, gmtime, strptime
from itertools import dropwhile

deadline = gmtime(time()-(60*60*24*7))
formatting = "%a %b %d %H:%M:%S %Y"

def not_recent(line):
    return strptime(line.split("[")[0].strip(), formatting) <= deadline

with open("/opt/CLiMB/Storage1/log/vsftp.log") as f:
    filename = time.strftime('%Y%m%d')+'.log'
    with open(filename,'w') as f1:
        for line in dropwhile(not_recent, f):
            print line
            f1.write(line)
于 2012-12-03T08:19:19.447 に答える
0

巨大なログファイルを扱っていることを考慮した別の実装

def tail(fname, n):
    fin = os.open(fname,os.O_RDONLY ) #Get an open file desc
    size = os.fstat(fin).st_size #Get the size from the stat
    fin = os.fdopen(fin) #Convert fd to file obj
    count = 0
    fin.seek(size) #Seek to the end of the file
    try:
        while count < n: #Loop until the count of newlines exceed the tail size
            pos = fin.tell() - 2 #Step backward
            if pos == -1: #Until you are past the begining
                raise StopIteration #When you end the Loop
            fin.seek(pos)
            if fin.read(1) == '\n': #And check if the next character is a new line
                count += 1 #Maintaining the count
    except StopIteration:
        pass

    return fin

使用法

for e in tail("Test.log",10):
    print e
于 2012-12-03T08:20:08.557 に答える