The best way to Get the Variety of Traces in a File in Python

If you want to get the variety of traces in a file, or the road rely whole from a file, utilizing Python, then you should use one of many following choices:

Choice 1 – Utilizing open() and sum()#

with open('listing/file.txt') as myfile:
    total_lines = sum(1 for line in myfile)

print(total_lines)

Choice 2 – Utilizing mmap#

import mmap

with open('listing/file.txt', "r+") as myfile:
    mm = mmap.mmap(myfile.fileno(), 0)
    total_lines = 0

    whereas mm.readline():
        total_lines += 1

print(total_lines)

Choice 3 – Utilizing file.learn()#

traces = 0
measurement = 1024 * 1024

with open(r'listing/file.txt', "r+") as myfile:
    read_file = myfile.learn

    buffer = read_file(measurement)
    
    whereas buffer:
        traces += buffer.rely('n')
        buffer = read_file(measurement)

if (traces != 0):
    traces += 1

print(traces)