title: Trigger loop after n lines in a text file
body: <p>I want to execute a loop if and only 5 lines have been executed inside the text file that's being written to. The reason being, I want the average to be calculated from the final 5 lines of the text file and if the program doesn't have 5 numbers to work with, then a rumtime error is thrown. </p>
  
  <pre><code>    #Imports
      from bs4 import BeautifulSoup
      from urllib import urlopen
      import time
  
      #Required Fields
      pageCount = 1290429
  
  
      #Loop
      logFile = open("PastWinners.txt", "r+")
      logFile.truncate()
      while(pageCount&gt;0): 
          time.sleep(1)
          html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read()
          soup = BeautifulSoup(html, "html.parser")
  
          try:
              section = soup.find('div', {"class":"row panel radius"})
              crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
              logFile.write(crashPoint[0:-1]+"\n")
          except:
              continue
  
          for i, line in enumerate(logFile):             #After 5 lines, execute this
              if i &gt; 4:
                  data = [float(line.rstrip()) for line in logFile]
                  print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:])))
              else:
                  continue
  
          print(crashPoint[0:-1])
          pageCount+=1
      logFile.close()
  
  If anyone knows the solution, or knows a better way to go about doing this, it would be helpful, thanks :).
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>Updated Code:</p>
  
  <pre><code>#Imports
  from bs4 import BeautifulSoup
  from urllib import urlopen
  import time
  
  #Required Fields
  pageCount = 1290429
  lineCount = 0
  
  def FindAverage():
      with open('PastWinners.txt') as logFile:
          data = [float(line.rstrip()) for line in logFile]
          print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:])))
  
  #Loop
  logFile = open("PastWinners.txt", "r+")
  logFile.truncate()
  while(pageCount&gt;0): 
      time.sleep(1)
      html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read()
      soup = BeautifulSoup(html, "html.parser")
  
      if lineCount &gt; 4:
          logFile.close()
          FindAverage()
      else:
          continue
  
      try:
          section = soup.find('div', {"class":"row panel radius"})
          crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
          logFile.write(crashPoint[0:-1]+"\n")
      except:
          continue
  
      print(crashPoint[0:-1])
      pageCount+=1
      lineCount+=1
  
  
  logFile.close()
  </code></pre>
  
  <p>New Problem:
  The program runs as expected, however once the average is calculated and displayed, the program doesn't loop again, it stops. I want it to work so after 5 lines it calculates the average and then displays the next number, then displays a new average and so on and so.</p>