Solution 1

def count_lines(filepath):
    with open(filepath) as fp:
        return len(fp.readlines())

Count Lines in File

Write a function that receives a path to a text file as parameter, and returns the amount of lines that text file has.

count_lines('test-file.txt')  # 10

Test Cases

test count lines - Run Test

import tempfile

def test_count_lines():
    fp = tempfile.NamedTemporaryFile(mode="w")
    fp.write('this is line 1\n')
    fp.write('this is line 2\n')
    fp.write('this is line 3\n')
    fp.flush()

    assert count_lines(fp.name) == 3

    fp.close()
def count_lines(filepath): pass