Wednesday, 15 July 2026

πŸš€ Day 87/150 – Count Lines in a File in Python

 



πŸš€ Day 87/150 – Count Lines in a File in Python

Counting the number of lines in a file is a common task in Python. It's useful for analyzing text files, processing datasets, validating file contents, and working with logs. Python provides several simple ways to count lines efficiently.

In this post, we'll explore four different methods to count the lines in a file.


Method 1 – Using a for Loop

Read the file line by line and increment a counter.

count = 0 with open("sample.txt", "r") as file: for line in file: count += 1 print("Total lines:", count)







Output
Total lines: 3


Explanation:
  • Initialize a counter with 0.
  • Iterate through each line in the file.
  • Increase the counter for every line.

Method 2 – Using readlines()

The readlines() method reads all lines into a list. The length of the list gives the total number of lines.

with open("sample.txt", "r") as file: lines = file.readlines() print("Total lines:", len(lines))





Output
Total lines: 3

Explanation:

  • readlines() returns a list of all lines.
  • len() counts the number of elements in the list.

Method 3 – Using sum()

A concise and memory-efficient approach.

with open("sample.txt", "r") as file: count = sum(1 for line in file) print("Total lines:", count)





Output
Total lines: 3

Explanation:

  • The generator expression produces 1 for each line.
  • sum() adds them together to get the total count.

Method 4 – Taking File Name from User

Allow the user to specify which file to count.

filename = input("Enter file name: ") with open(filename, "r") as file: count = sum(1 for line in file) print("Total lines:", count)







Sample Input
sample.txt

Output
Total lines: 3

Explanation:

  • Accepts a file name from the user.
  • Counts the number of lines dynamically.

Comparison of Methods

MethodBest For
for LoopUnderstanding the counting logic
readlines()Small files
sum()Fast and memory-efficient
User InputInteractive programs

πŸ”₯ Key Takeaways

  • A for loop is the easiest way to understand how line counting works.
  • readlines() is suitable for small files but loads the entire file into memory.
  • sum(1 for line in file) is a clean and efficient way to count lines.
  • Always use the with statement to ensure files are closed automatically.
  • Counting lines is useful for file analysis, log processing, and data validation.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (309) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (282) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (9) Data Analysis (40) Data Analytics (27) data management (16) Data Science (393) Data Strucures (23) Deep Learning (198) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (23) Finance (11) flask (4) flutter (1) FPL (17) Generative AI (76) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (349) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1404) Python Coding Challenge (1189) Python Mathematics (5) Python Mistakes (51) Python Quiz (570) Python Tips (25) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)