Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get line count of a large file cheaply in Python?

I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most efficient way both memory- and time-wise?

At the moment I do:

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

is it possible to do any better?

like image 692
SilentGhost Avatar asked May 10 '09 10:05

SilentGhost


People also ask

How do you count lines of code in Python?

To count all the lines of code in the files in a directory, call the "countIn" function, passing the directory as a parameter.

How do I count the number of lines in a file?

The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

How do you get a file line in Python?

Method 1: Read a File Line by Line using readlines() This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines. We can iterate over the list and strip the newline '\n' character using strip() function. Example: Python3.


2 Answers

One line, probably pretty fast:

num_lines = sum(1 for line in open('myfile.txt')) 
like image 168
Kyle Avatar answered Oct 01 '22 17:10

Kyle


You can't get any better than that.

After all, any solution will have to read the entire file, figure out how many \n you have, and return that result.

Do you have a better way of doing that without reading the entire file? Not sure... The best solution will always be I/O-bound, best you can do is make sure you don't use unnecessary memory, but it looks like you have that covered.

like image 39
Yuval Adam Avatar answered Oct 01 '22 18:10

Yuval Adam