def count_lines(filepath): with open(filepath) as fp: return len(fp.readlines())
Time is over! You can keep submitting you assignments, but they won't compute for the score of this quiz.
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 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()