Saturday, March 11, 2023
HomeSoftware EngineeringThe way to Get the Variety of Traces in a File in...

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


If you must get the variety of traces in a file, or the road depend complete from a file, utilizing Python, then you need to use one of many following choices:

Possibility 1 – Utilizing open() and sum()

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

print(total_lines)

Possibility 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)

Possibility 3 – Utilizing file.learn()

traces = 0
dimension = 1024 * 1024

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

    buffer = read_file(dimension)
    
    whereas buffer:
        traces += buffer.depend('n')
        buffer = read_file(dimension)

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

print(traces)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments