Skip to content Skip to sidebar Skip to footer

Counting Non-empty Lines And Sum Of Lengths Of Those Lines In Python

Am trying to create a function that takes a filename and it returns a 2-tuple with the number of the non-empty lines in that program, and the sum of the lengths of all those lines.

Solution 1:

A fairly simple approach is to build a generator to strip trailing whitespace, then enumerate over that (with a start value of 1) filtering out blank lines, and summing the length of each line in turn, eg:

defcode_metric(filename):
    line_count = char_count = 0withopen(filename) as fin:
        stripped = (line.rstrip() for line in fin)
        for line_count, line inenumerate(filter(None, stripped), 1):
            char_count += len(line)
    return line_count, char_count

print(code_metric('cmtest.py'))
# (3, 85)

Solution 2:

In order to count lines, maybe this code is cleaner:

withopen(file) as f:
    lines = len(file.readlines())

For the second part of your program, if you intend to count only non-empty characters, then you forgot to remove '\t' and '\n'. If that's the case

with open(file) as f:
    num_chars = len(re.sub('\s', '', f.read()))

Some people have advised you to do both things in one loop. That is fine, but if you keep them separated you can make them into different functions and have more reusability of them that way. Unless you are handling huge files (or executing this coded millions of times), it shouldn't matter in terms of performance.

Post a Comment for "Counting Non-empty Lines And Sum Of Lengths Of Those Lines In Python"