I'd like to ask you a question about how to determine the number of file lines

Asked 2 years ago, Updated 2 years ago, 88 views

It's the same as the title. You need to figure out the number of lines in a very large file.

I can only think of how to read all the files I think it's too inefficient.

Please tell me how to use less memory and time.

python text-file line-number

2022-09-22 22:34

1 Answers

No matter what you do, you have to read everything in the file. The following is the best way to remove unnecessary memory.

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1


2022-09-22 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.