Tech Blog

Coding

How to fetch unread lines from a large file that is dynamically adding logs using python?

By Thanuja2 min read
← Back to blog

Analysts often depend on log files to dive deeper into the issues that break systems. Handling huge log files that are more than 10GB line by line and seeking the RCA is hectic as well as time-consuming.

Here is one way to get unread lines from a large file where logs are being added every second from the server.

A text file linecount.txt stores the number of lines that are read from logfile.txt. So for example, if there are 5000 lines in logfile.txt and it is being read for the first time. There will be 0 stored in linecount.txt as the file was never read before. When the code is run, it reads from the first line to 4999 lines and stores the count “5000” in the text file linecount.txt. Post this if 100 more lines are added to logfile.txt, and when we run the code again it reads from 5000th line to 5099 line and stores the latest count i.e, 5100.

#checks if linecount.txt file exists. 
#if it doesn't exist a file is created and 0 is stored.
linecountpath = "D:\\linecount.txt"
fileA = "D:\\logfile.txt"
my_file = Path(linecountpath)
if my_file.is_file():
    #donothing
else:
    with open (linecountpath, "w") as lc:
        lc.write("0")
       
#read lines from file A from nth line
    with open(fileA, 'r', errors='ignore') as f:
        with open (linecountpath, "r") as lc:
             b=lc.read()
            for line in islice(f, n, None):
                line = line.replace('<br />')
               #do what you want with unreadlines here.

#getting the nth line number form a large file 
def length_file(filename):
    with open(filename) as f:
        for i, l in enumerate(f):
            pass
    return i + 1
#writing line count into linecount.txt 
with open (linecountpath, "r+") as lc:
    if os.stat(linecountpath).st_size == 0:
        lc.truncate(0)
        lc.seek(0)
        lc.write("0")
        else:
            i = length_file(fileA)
            lc.truncate(0)
            lc.seek(0)
            lc.write(str(i))
            

There are many other efficient ways to open a large file. You can check few ways in the below link https://stackoverflow.com/questions/845058/how-to-get-line-count-of-a-large-file-cheaply-in-python 

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →