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
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
© 2025 OneMinuteCode. All rights reserved.