Python Count Lines
readlines()Practice

How many lines are in the file?

Open the file, read all lines, print len(lines). Empty files count as 0.

Picture this: one card per line

Each newline becomes one list item. Count the items.

open()file
→
readlines()list
→
len(...)count

In easy words

A line is everything up to a newline. readlines() gives you a list you can measure.

Where this is used

Log size checks, CSV row counts, and progress bars over files.

Run algorithm

Each step highlights the matching Python line. Program output appears only when a line that prints actually runs.

Ready
input.txt
1with open("input.txt", "r") as f:
2lines = f.readlines()
3print(len(lines))
Output:

Example Program

1with open("input.txt", "r", encoding="utf-8") as f:
2print(len(f.readlines()))