Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to search the last X lines of a file?

I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:

s = "foo" last_bit = fileObj.readlines()[-10:] for line in last_bit:     if line == s:         print "FOUND" 
like image 425
Harley Holcombe Avatar asked Nov 03 '08 23:11

Harley Holcombe


People also ask

How do I read the last 5 lines of a file in Python?

As we know, Python provides multiple in-built features and modules for handling files. Let's discuss different ways to read last N lines of a file using Python. In this approach, the idea is to use a negative iterator with the readlines() function to read all the lines requested by the user from the end of file.

Which function will return the list of all the lines of the file?

Python File readlines() Method The readlines() method returns a list containing each line in the file as a list item.


1 Answers

# Tail from __future__ import with_statement  find_str = "FIREFOX"                    # String to find fname = "g:/autoIt/ActiveWin.log_2"     # File to check  with open(fname, "r") as f:     f.seek (0, 2)           # Seek @ EOF     fsize = f.tell()        # Get Size     f.seek (max (fsize-1024, 0), 0) # Set pos @ last n chars     lines = f.readlines()       # Read to end  lines = lines[-10:]    # Get last 10 lines  # This returns True if any line is exactly find_str + "\n" print find_str + "\n" in lines  # If you're searching for a substring for line in lines:     if find_str in line:         print True         break 
like image 174
PabloG Avatar answered Oct 02 '22 14:10

PabloG