title: Read previous line in a file python
body: <p>I need to get the value of the previous line in a file and compare it with the current line as I'm iterating through the file. The file is HUGE so I can't read it whole or randomly accessing a line number with <code>linecache</code> because the library function still reads the whole file into memory anyway. </p>
  
  <p><strong>EDIT</strong> I'm so sorry I forgot the mention that I have to read the file backwardly.</p>
  
  <p><strong>EDIT2</strong> </p>
  
  <p>I have tried the following:</p>
  
  <pre><code> f = open("filename", "r")
   for line in reversed(f.readlines()): # this doesn't work because there are too many lines to read into memory
  
   line = linecache.getline("filename", num_line) # this also doesn't work due to the same problem above. 
  </code></pre>