6
Lesson 6
Iterating through a file
Objective
By the end of this lesson, students will understand how to efficiently read file content line by line using loops in Python. They will learn to use for loops and while loops to process files sequentially without loading the entire file into memory.
1. Introduction to Iterating through a file:
Reading files line by line is a common and memory-efficient way to process large files in Python. Instead of loading all data at once, iterating through a file allows you to handle each line individually. This approach is ideal for tasks like log processing and text analysis.
2. Using a for loop to read lines:
A for loop is one of the most straightforward ways to iterate through a file line by line. When a file is opened in read mode, you can loop over it directly, treating each line as a separate string.
By the end of this lesson, students will understand how to efficiently read file content line by line using loops in Python. They will learn to use for loops and while loops to process files sequentially without loading the entire file into memory.
1. Introduction to Iterating through a file:
Reading files line by line is a common and memory-efficient way to process large files in Python. Instead of loading all data at once, iterating through a file allows you to handle each line individually. This approach is ideal for tasks like log processing and text analysis.
2. Using a for loop to read lines:
A for loop is one of the most straightforward ways to iterate through a file line by line. When a file is opened in read mode, you can loop over it directly, treating each line as a separate string.
with open("example.txt", "r") as file: for line in file: print(line, end="") # Prints each line as read from the file
This method reads the file one line at a time, which is efficient and conserves memory, as it doesn’t load the entire file at once.
3. Using a while loop with readline()
The while loop can also be used with the readline() method to read each line until the end of the file. This approach is especially useful if you want more control over the iteration process.
with open("example.txt", "r") as file: line = file.readline() while line: print(line, end="") # Prints each line line = file.readline()
Here, the loop continues reading lines until readline() returns an empty string, indicating the end of the file.
4. Practical Tips: Managing large files
When working with large files, consider the following:
- Avoid loading entire files: Iterating line by line is memory-efficient.
- Use context managers: Always use with open(...) to ensure the file closes automatically after reading.
- Avoid Accidental Newlines: Using print(line, end="") helps avoid extra newlines when reading.
5. Practical examples and exercises:
Exercise 1: Basic File Iteration
1. Open a text file and use a for loop to print each line in the file.
Exercise 2: Counting Lines in a File
1. Use a for loop to count and print the total number of lines in a file.
Exercise 3: Reading until a condition is met
1. Open a file and use a while loop with readline() to read lines until a specific word or phrase is found.
2. Stop reading once the word/phrase is located and print the matching line.
Exercise 4: Selective line processing
1. Open a file and use a for loop to print only lines that contain a specific keyword.
2. Skip lines that don’t contain the keyword.
Exercise 5: Aggregating line content
1. Use a for loop to iterate over each line and accumulate a count of a specific character (e.g., "a") across the entire file.
Conclusion
In this lesson, students learned how to read a file line by line using both for loops and while loops with readline(). This approach to file iteration is essential for handling large files efficiently and allows for selective processing of file content. Understanding these techniques equips students with practical skills for reading and processing files in Python.