count_lines(path) that opens a text file and returns how many lines it contains. Use a with statement so the file is always closed, and iterate the file object directly instead of loading everything with read().count_lines('app.log') # file has 3 lines3
Iterating a file object yields one line at a time.
for line in f: consumes the file lazily.
sum(1 for _ in f) is an idiomatic count.