2

How to get latest timestamp from a log file in python3. The log file could be in mb's and sometimes in gb's.

eg : The format is 2017-02-13 17:58:38


2017-02-13 20:07:17 [HTTP-9] DEBUG 
2017-02-17 20:07:18 [HTTP-9] DEBUG 
2017-02-20 20:07:18 [HTTP-9] DEBUG 
.
.
skyrocker
  • 199
  • 2
  • 8
  • 20
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Feb 22 '17 at 18:58
  • 2
    I'm voting to close this question as off-topic because SO is not a coding service. – TigerhawkT3 Feb 22 '17 at 18:58
  • 2
    And from the section of the file you've shown, all you need is the last line and you could get that with a simple `tail -1 filename.txt`. – TigerhawkT3 Feb 22 '17 at 19:02

1 Answers1

0

One way is using a collections.deque with max size 1 in order to get the last line then use str.split() method in order to extract the time:

from collections import deque
with open(file_name) as f:
    last_line = deque(f, maxlen=1).pop()

You can also use itertools.dropwhile() in order to drop the lines of the file object (which is an iterator-like object) until they meet a certain criteria like based on the ends of lines.

from itertools import dropwhile
with open(file_name) as f:
    last_line = next(dropwhile(lambda x: not x.endswith('G'), f))
    # note that in aforementioned function supposed that all the lines ends with G (apparently, but all except the last line ends with newline) 

In both method you van get the time like following:

time_string = last_line.rsplit(' ', 2)[0]

Or if you want to convert to time object, or timestamp:

from datetime import datetime
datetime.strptime(time_string, '%Y-%m-%d %H:%M:%S')
datetime.strptime(a, '%Y-%m-%d %H:%M:%S').timestamp()
Mazdak
  • 105,000
  • 18
  • 159
  • 188