Count the number of lines in a txt file using Python, excluding empty lines

I want to count the number of lines in a .txt file that looks something like this:

apple orange pear hippo donkey 

If empty lines are used to separate blocks. The result I'm looking for based on the above example is five (rows).

How can I achieve this?

As a bonus, it would be nice to know how many blocks / paragraphs there are. So, based on the above example, it will be two blocks.

+8
source share
10 answers
 non_blank_count = 0 with open('data.txt') as infp: for line in infp: if line.strip(): non_blank_count += 1 print 'number of non-blank lines found %d' % non_blank_count 

UPDATE: re-read the question, OP wants to count non-empty lines .. (sigh .. thanks @RanRag). (I need a break from the computer ...)

+19
source

A short way to count the number of non-empty lines can be:

 with open('data.txt', 'r') as f: lines = f.readlines() num_lines = len([l for l in lines if l.strip(' \n') != '']) 
+3
source
 sum([1 for i in open("file_name","r").readlines() if i.strip()]) 
+1
source

Given that empty lines only contain the new line character, it would be much faster to avoid calling str.strip , which creates a new line, but instead checks to see if the line contains only spaces with str.isspace , and then skips it:

 with open('data.txt') as f: non_blank_lines = sum(not line.isspace() for line in f) 

Demo:

 from io import StringIO s = '''apple orange pear hippo donkey''' non_blank_lines = sum(not line.isspace() for line in StringIO(s))) # 5 

You can use str.isspace with itertools.groupby to count the number of contiguous lines / blocks in a file:

 from itertools import groupby no_paragraphs = sum(k for k, _ in groupby(StringIO(s), lambda x: not x.isspace())) print(no_paragraphs) # 2 
+1
source

Not empty lines Counter:

 lines_counter = 0 with open ('test_file.txt') as f: for line in f: if line != '\n': lines_counter += 1 

Block Counter:

 para_counter = 0 prev = '\n' with open ('test_file.txt') as f: for line in f: if line != '\n' and prev == '\n': para_counter += 1 prev = line 
0
source

This bit of Python code should solve your problem:

 with open('data.txt', 'r') as f: lines = len(list(filter(lambda x: x.strip(), f))) 
0
source

Here is how I would do it:

 f = open("file.txt") l = [x for x in f.readlines() if x != "\n"] print len(l) 

readlines() will make a list of all lines in the file, and then you can simply take those lines that have at least something in them. It looks pretty simple to me!

0
source

Pretty direct! I believe

 f = open('path','r') count = 0 for lines in f: if lines.strip(): count +=1 print count 
0
source

I am surprised that there is no pure pythonic answer yet (as of January 1, 2019). Many of the other answers create unnecessary lists, count in a non-pythonic way, loop the lines of the file in a non-pythonic way, do not close the file properly, do unnecessary things, suggest that the line terminator can only be '\n' or have other minor problems.

Here is my suggested solution:

 with open('myfile.txt') as f: line_count = sum(1 for line in f if line.strip()) 

The question does not determine what an empty string is. My definition of an empty string: line is an empty string if and only if line.strip() returns an empty string. This may or may not be your definition of an empty string.

0
source
 fhand = open("file_name",encoding="utf8") count = 0 for line in fhand: line = line.rstrip() line = line.lstrip() if len(line)>0 : count = count + 1 print("countline", count) 
0
source

All Articles