title: How to print 3rd from bottom line in file?
body: <p>I want to read and print the 3rd from last line in date.log.  This is specified in the line_from_bottom_line variable.
  The log can have any number of lines at any time.</p>
  
  <p>Here is an example of what the log looks like:</p>
  
  <pre><code>192.168.80.231 May 8 2018 18:45:00
  192.168.80.231 July 30 2018 09:46:48
  192.168.80.231 July 2 2018 14:37:14
  </code></pre>
  
  <p>If there are only 3 lines in the log, the else line will be printed:</p>
  
  <pre><code> line_number[x1] = [(index,log_time)]
  </code></pre>
  
  <p>The output will be:</p>
  
  <pre><code>(1, '18:45:00')
  </code></pre>
  
  <p>Which is not what I want.</p>
  
  <p>If there are 4 or more lines, the line printed will be in the format of:</p>
  
  <pre><code>2018:8:18:45:00
  </code></pre>
  
  <p>This is what I want.</p>
  
  <p>I think my code below is moving to the bottom line and subtracting 3. So if 4 lines don't exist, it doesn't know
  what to print.  How can I change it so that the 3rd from bottom line is printed even if there are not 4 or more 
  lines in the log?</p>
  
  <pre><code>old_time = (line_number[x1][-(line_from_bottom_line)].__str__())
  
  from datetime import datetime, date, time
  
  # this would be the third from last line
  line_from_bottom_line = 3
  
  date_order  = 2
  time_order  = 4
  year_order  = 3
  
  present = datetime.now()
  
  def main():
      logfile = open('krinkov.log', 'r+')
      line_number = dict()
      for index,line in enumerate(logfile,1):  # scan lines
          if line in ['\n', '\r\n']:  # Error Checking: if not enough lines in var .log
              print("Not enough lines.")
              return
          if line:
              x1 = line.split()[0]  # if line, get IP address
              log_day   = line.split()[date_order]
              log_time  = line.split()[time_order]  # This will already be in the format of hh:mm:ss
              log_year  = line.split()[year_order]
          if x1 in line_number :  # if ip address on line
              line_number[x1].append((log_year + ":" + log_day + ":" + log_time))
  
          else:
              line_number[x1] = [(index,log_time)]
  
      if x1 in line_number and len(line_number.get(x1,None)) &gt; 1:
  
          # Below is where I am having issues.
          # If there are not 4 or more lines in the log, an error occurs.
          old_time = (line_number[x1][-line_from_bottom_line])
          print(old_time)
  
          # ** get last line number.  Print that line number.  then subtract 2 from it
          # old_time = that new number
  
  
      else:
          print('Nothing')
  main()
  </code></pre>