Introduction
Reading a file line by line is a common task in Python, especially when dealing with large files or when you want to process each line separately. Python provides several methods to read a file line by line, which allows you to handle files efficiently without loading the entire file into memory. This tutorial will guide you through creating a Python program that reads a file line by line.
Example:
- File Content (
example.txt
):Line 1: Hello, World! Line 2: Welcome to Python file handling. Line 3: Reading files line by line is useful.
- Program Output:
Line 1: Hello, World! Line 2: Welcome to Python file handling. Line 3: Reading files line by line is useful.
Problem Statement
Create a Python program that:
- Opens a file for reading.
- Reads and prints each line of the file separately.
- Closes the file after reading.
Solution Steps
- Specify the File Name: Provide the name of the file to be read.
- Open the File: Use the
open()
function to open the file in read mode. - Read the File Line by Line: Use a loop to read each line of the file.
- Display Each Line: Print each line as it is read from the file.
- Close the File: Ensure the file is properly closed after reading.
Python Program
# Python Program to Read a File Line by Line
# Author: https://www.rameshfadatare.com/
# Step 1: Specify the file name
file_name = "example.txt"
# Step 2: Open the file in read mode
try:
with open(file_name, "r") as file:
# Step 3: Read the file line by line
print("File Content:\n")
for line in file:
# Step 4: Display each line
print(line, end="") # end="" avoids adding extra newlines
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Explanation
Step 1: Specify the File Name
- The variable
file_name
is assigned the name of the file to be read. Make sure the file exists in the same directory as the Python script, or provide the full path to the file.
Step 2: Open the File in Read Mode
- The
open()
function is used to open the file in read mode ("r"
). Thewith
statement ensures that the file is automatically closed after reading, even if an error occurs.
Step 3: Read the File Line by Line
- A
for
loop is used to iterate over the file object. This loop reads the file line by line.
Step 4: Display Each Line
- The
print()
function is used to display each line as it is read from the file. Theend=""
argument is used to avoid adding extra newlines because each line read from the file already ends with a newline character.
Step 5: Handle Exceptions
- The program includes exception handling using
try-except
blocks to catch and handle errors such asFileNotFoundError
if the file does not exist, orIOError
for general input/output errors.
Output Example
Example Output:
File Content:
Line 1: Hello, World!
Line 2: Welcome to Python file handling.
Line 3: Reading files line by line is useful.
Additional Examples
Example 1: Reading Large Files Efficiently with readline()
# Reading large files efficiently with readline()
file_name = "large_file.txt"
try:
with open(file_name, "r") as file:
print("Reading large file line by line:\n")
while True:
line = file.readline()
if not line:
break
print(line, end="")
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Output:
- The program reads each line one at a time until the end of the file is reached.
Example 2: Storing Lines in a List While Reading
# Storing lines in a list while reading
file_name = "example.txt"
lines = []
try:
with open(file_name, "r") as file:
print("Storing lines in a list:\n")
for line in file:
lines.append(line.strip()) # strip() removes leading/trailing whitespace
print(lines)
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Output:
Storing lines in a list:
['Line 1: Hello, World!', 'Line 2: Welcome to Python file handling.', 'Line 3: Reading files line by line is useful.']
Example 3: Counting Lines While Reading
# Counting lines in a file
file_name = "example.txt"
line_count = 0
try:
with open(file_name, "r") as file:
print("Counting lines in the file:\n")
for line in file:
line_count += 1
print(f"Line {line_count}: {line.strip()}")
print(f"\nTotal number of lines: {line_count}")
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Output:
Counting lines in the file:
Line 1: Hello, World!
Line 2: Welcome to Python file handling.
Line 3: Reading files line by line is useful.
Total number of lines: 3
Conclusion
This Python program demonstrates how to read a file line by line using the open()
function and a for
loop. The program also includes exception handling to manage errors that may occur during file operations, making it robust for real-world applications. Understanding how to read files line by line is essential for tasks involving large files, log processing, or any scenario where you need to process data incrementally in Python.