[Tutor] look back comprehensively

Steven D'Aprano steve at pearwood.info
Wed Nov 14 17:01:31 EST 2018


On Tue, Nov 13, 2018 at 11:59:24PM -0500, Avi Gross wrote:
> I have been thinking about the thread we have had where the job seemed to be
> to read in a log file and if some string was found, process the line before
> it and generate some report. Is that generally correct?

If that description is correct, then the solution is trivial: iterate 
over the file, line by line, keeping the previous line:

previous_line = None
for current_line in file:
    process(current_line, previous_line)
    previous_line = current_line


No need for complex solutions, or memory-hungry solutions that require 
reading the entire file into memory at once (okay for, say, a million 
lines, but not if your logfile is 2GB in size). If you need the line 
number:

previous_line = None
for line_num, current_line in enumerate(file, 1):
    process(current_line, previous_line)
    previous_line = current_line


> Use just the readlines version to get a list of strings representing each
> line. 

That requires reading the entire file into memory at once. That may be 
acceptable if your file is guaranteed to be small, but since log files 
can grow big enough to fill hard drives, that might not be a good 
assumption for serious production-quality scripts.


-- 
Steve


More information about the Tutor mailing list