Counting lines

Let's break from the TDD rules and move a bit faster now. First we'll update the function to return the number of lines in the file:

# text_analyzer.py

def analyze_text(filename):
"""Calculate the number of lines and characters in a file.

Args:
filename: The name of the file to analyze.

Raises:
IOError: If ``filename`` does not exist or can't be read.

Returns: The number of lines in the file.
"""
with open(filename, 'r') as f:
return sum(1 for _ in f)

This change indeed gives us the results we want:

% python text_analyzer.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset