Skip to content

FILE ITERATION PATTERNS - Different ways to iterate through files

Python
#!/usr/bin/env python3
"""
FILE ITERATION PATTERNS - Different ways to iterate through files
Demonstrates various iteration techniques and patterns
"""

import os
import tempfile

print("=" * 60)
print("FILE ITERATION PATTERNS - Various Reading Strategies")
print("=" * 60)

temp_dir = tempfile.gettempdir()

# Create sample file
sample_file = os.path.join(temp_dir, "iteration_demo.txt")
with open(sample_file, 'w') as f:
    for i in range(1, 21):
        f.write(f"Line {i:02d}: This is sample content for iteration examples\n")

print("Created sample file with 20 lines\n")

# Example 1: Basic line iteration
print("1. Basic Line Iteration")
print("-" * 40)
with open(sample_file, 'r') as f:
    count = 0
    for line in f:
        count += 1
        if count <= 3:
            print(f"  {line.rstrip()}")
    print(f"  ... (total {count} lines)")

# Example 2: Enumerate for line numbers
print("\n2. Enumerate for Line Numbers")
print("-" * 40)
with open(sample_file, 'r') as f:
    for line_num, line in enumerate(f, 1):
        if line_num <= 3:
            print(f"  Line {line_num}: {line.rstrip()}")

# Example 3: Read with while loop
print("\n3. While Loop with readline()")
print("-" * 40)
with open(sample_file, 'r') as f:
    count = 0
    while True:
        line = f.readline()
        if not line:
            break
        count += 1
        if count <= 3:
            print(f"  {line.rstrip()}")
    print(f"  ... (total {count} lines)")

# Example 4: Skip lines
print("\n4. Skip First N Lines")
print("-" * 40)
skip = 5
with open(sample_file, 'r') as f:
    for i, line in enumerate(f):
        if i < skip:
            continue
        if i < skip + 3:
            print(f"  {line.rstrip()}")

# Example 5: Read every Nth line
print("\n5. Read Every 5th Line")
print("-" * 40)
with open(sample_file, 'r') as f:
    for i, line in enumerate(f, 1):
        if i % 5 == 0:
            print(f"  Line {i}: {line.rstrip()}")

# Example 6: Read lines in batches
print("\n6. Read Lines in Batches of 5")
print("-" * 40)
batch_size = 5
with open(sample_file, 'r') as f:
    batch_num = 0
    while True:
        batch = []
        for _ in range(batch_size):
            line = f.readline()
            if not line:
                break
            batch.append(line.rstrip())

        if not batch:
            break

        batch_num += 1
        print(f"  Batch {batch_num}: {len(batch)} lines")
        if batch_num == 1:
            for line in batch:
                print(f"    {line}")

# Example 7: Filter while iterating
print("\n7. Filter Lines Containing 'Line 1'")
print("-" * 40)
with open(sample_file, 'r') as f:
    filtered = [line.rstrip() for line in f if 'Line 1' in line]

print(f"  Found {len(filtered)} matching lines:")
for line in filtered[:5]:
    print(f"    {line}")

# Example 8: Two-pass iteration
print("\n8. Two-Pass Iteration (Count, then Process)")
print("-" * 40)
# First pass: count
with open(sample_file, 'r') as f:
    total_lines = sum(1 for _ in f)

# Second pass: process with progress
with open(sample_file, 'r') as f:
    for i, line in enumerate(f, 1):
        if i <= 3:
            progress = (i / total_lines) * 100
            print(f"  [{progress:5.1f}%] {line.rstrip()}")

# Example 9: Sliding window iteration
print("\n9. Sliding Window (3 lines)")
print("-" * 40)
window_size = 3
with open(sample_file, 'r') as f:
    window = []

    for line in f:
        window.append(line.rstrip())

        if len(window) == window_size:
            if len(window) == window_size and window[0].startswith("Line 03"):
                print(f"  Window at Line 03:")
                for i, w_line in enumerate(window):
                    print(f"    [{i}] {w_line}")

            window.pop(0)  # Slide window

# Example 10: Pairs of lines
print("\n10. Process Lines in Pairs")
print("-" * 40)
with open(sample_file, 'r') as f:
    lines = f.readlines()

pairs_shown = 0
for i in range(0, len(lines) - 1, 2):
    if pairs_shown < 2:
        print(f"  Pair {pairs_shown + 1}:")
        print(f"    Line A: {lines[i].rstrip()}")
        print(f"    Line B: {lines[i+1].rstrip()}")
        pairs_shown += 1

# Example 11: Conditional iteration
print("\n11. Stop Iteration on Condition")
print("-" * 40)
with open(sample_file, 'r') as f:
    for line in f:
        print(f"  {line.rstrip()}")
        if "Line 05" in line:
            print("  ... stopping at Line 05")
            break

# Example 12: Reverse iteration
print("\n12. Iterate Lines in Reverse")
print("-" * 40)
with open(sample_file, 'r') as f:
    lines = f.readlines()

for i, line in enumerate(reversed(lines[-3:]), 1):
    print(f"  {line.rstrip()}")

# Example 13: Iterator with state
print("\n13. Stateful Iterator")
print("-" * 40)
class FileLineIterator:
    def __init__(self, filename):
        self.filename = filename
        self.count = 0

    def __iter__(self):
        with open(self.filename, 'r') as f:
            for line in f:
                self.count += 1
                yield (self.count, line.rstrip())

iterator = FileLineIterator(sample_file)
for line_num, line in iterator:
    if line_num <= 3:
        print(f"  {line_num}: {line}")
print(f"  Total lines iterated: {iterator.count}")

# Example 14: Chunk reading (bytes)
print("\n14. Chunk-Based Iteration (1024 bytes)")
print("-" * 40)
chunk_size = 1024
chunks_read = 0
with open(sample_file, 'rb') as f:
    while True:
        chunk = f.read(chunk_size)
        if not chunk:
            break
        chunks_read += 1

print(f"  Read {chunks_read} chunks of {chunk_size} bytes")

# Example 15: Multiple files iteration
print("\n15. Iterate Multiple Files")
print("-" * 40)
file2 = os.path.join(temp_dir, "file2.txt")
with open(file2, 'w') as f:
    f.write("File 2 Line 1\n")
    f.write("File 2 Line 2\n")

files_to_process = [sample_file, file2]
for filename in files_to_process:
    print(f"\n  Processing: {os.path.basename(filename)}")
    with open(filename, 'r') as f:
        for i, line in enumerate(f, 1):
            if i <= 2:
                print(f"    {line.rstrip()}")

# Cleanup
for f in [sample_file, file2]:
    if os.path.exists(f):
        os.remove(f)

print("\n" + "=" * 60)
print("Key Points:")
print("  - Direct iteration: for line in file")
print("  - enumerate() for line numbers")
print("  - Can skip, filter, or batch lines")
print("  - Sliding windows for context")
print("  - reversed() for backward iteration")
print("=" * 60)