Menu

Python Topic

Python File Handling

1. Basic File Operations

The File Handling Workflow:

  1. Open the file

  2. Read from or write to the file

  3. Close the file

Opening Files:

python

# Basic syntax
file = open("filename.txt", "mode")

File Modes:

Mode Description
'r' Read (default) - opens for reading
'w' Write - creates new file or overwrites existing
'a' Append - adds to end of file, creates if doesn't exist
'x' Create - creates file, error if exists
'r+' Read and write
'b' Binary mode (e.g., 'rb', 'wb')

2. Reading Files

Reading Entire File:

python

# Method 1: Using read() - reads entire content
with open("example.txt", "r") as file:
    content = file.read()
    print(content)

# Method 2: Using readlines() - returns list of lines
with open("example.txt", "r") as file:
    lines = file.readlines()
    for line in lines:
        print(line.strip())  # strip() removes newline characters

Reading Line by Line:

python

# Most memory-efficient for large files
with open("example.txt", "r") as file:
    for line in file:
        print(f"Line: {line.strip()}")

# Reading specific number of characters
with open("example.txt", "r") as file:
    first_100_chars = file.read(100)
    print(first_100_chars)

The with Statement (Recommended):

python

# Using 'with' automatically closes the file
with open("example.txt", "r") as file:
    content = file.read()
# File is automatically closed here

# Without 'with' - you must close manually
file = open("example.txt", "r")
content = file.read()
file.close()  # Don't forget this!

3. Writing Files

Writing to Files:

python

# Writing (overwrites existing file or creates new)
with open("output.txt", "w") as file:
    file.write("Hello, World!\n")
    file.write("This is a new line.\n")
    file.write("And another line.")

# Writing multiple lines at once
lines = ["First line\n", "Second line\n", "Third line\n"]
with open("output.txt", "w") as file:
    file.writelines(lines)

Appending to Files:

python

# Appending (adds to end of file)
with open("output.txt", "a") as file:
    file.write("\nThis line was appended.\n")
    file.write("So was this one!\n")

4. Working with Text Files

Example 1: Simple Text File Processor

python

def process_text_file(filename):
    """Read a text file and perform basic analysis."""
    try:
        with open(filename, "r", encoding="utf-8") as file:
            content = file.read()
            
            # Basic analysis
            lines = content.split('\n')
            words = content.split()
            characters = len(content)
            
            print(f"File: {filename}")
            print(f"Lines: {len(lines)}")
            print(f"Words: {len(words)}")
            print(f"Characters: {characters}")
            
            # Find longest word
            if words:
                longest_word = max(words, key=len)
                print(f"Longest word: '{longest_word}' ({len(longest_word)} chars)")
            
    except FileNotFoundError:
        print(f"Error: File '{filename}' not found.")
    except Exception as e:
        print(f"Error: {e}")

# Usage
process_text_file("sample.txt")

Example 2: Log File Processor

python

def filter_logs(input_file, output_file, keyword):
    """Filter log entries containing a specific keyword."""
    try:
        with open(input_file, "r") as infile, open(output_file, "w") as outfile:
            outfile.write(f"Log entries containing '{keyword}':\n")
            outfile.write("=" * 50 + "\n")
            
            for line_num, line in enumerate(infile, 1):
                if keyword.lower() in line.lower():
                    outfile.write(f"Line {line_num}: {line}")
            
        print(f"Filtered logs written to {output_file}")
        
    except FileNotFoundError:
        print("Error: Input file not found")

# Usage
filter_logs("application.log", "error_logs.txt", "ERROR")

5. Working with CSV Files

CSV (Comma-Separated Values) files are commonly used for data exchange.

Using the csv Module:

python

import csv

Reading CSV Files:

python

# Reading CSV as lists
def read_csv_as_lists(filename):
    with open(filename, "r", newline="") as file:
        reader = csv.reader(file)
        header = next(reader)  # Read header row
        print(f"Headers: {header}")
        
        for row in reader:
            print(f"Row: {row}")

# Reading CSV as dictionaries
def read_csv_as_dicts(filename):
    with open(filename, "r", newline="") as file:
        reader = csv.DictReader(file)
        for row in reader:
            print(f"Name: {row['name']}, Age: {row['age']}, City: {row['city']}")

# Usage
read_csv_as_lists("data.csv")
read_csv_as_dicts("data.csv")

Writing CSV Files:

python

# Writing CSV from lists
def write_csv_from_lists(filename, data):
    with open(filename, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerow(["Name", "Age", "City"])  # Header
        writer.writerows(data)  # Write all rows

# Writing CSV from dictionaries
def write_csv_from_dicts(filename, data):
    with open(filename, "w", newline="") as file:
        fieldnames = ["name", "age", "city"]
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        writer.writeheader()  # Write header
        writer.writerows(data)  # Write all rows

# Sample data
list_data = [
    ["Alice", "25", "New York"],
    ["Bob", "30", "London"],
    ["Charlie", "35", "Tokyo"]
]

dict_data = [
    {"name": "Alice", "age": "25", "city": "New York"},
    {"name": "Bob", "age": "30", "city": "London"},
    {"name": "Charlie", "age": "35", "city": "Tokyo"}
]

write_csv_from_lists("output_list.csv", list_data)
write_csv_from_dicts("output_dict.csv", dict_data)

6. Practical Examples

Example 1: Configuration File Handler

python

def read_config(config_file):
    """Read a simple configuration file (key=value format)."""
    config = {}
    try:
        with open(config_file, "r") as file:
            for line_num, line in enumerate(file, 1):
                line = line.strip()
                # Skip empty lines and comments
                if not line or line.startswith("#"):
                    continue
                
                # Parse key=value pairs
                if "=" in line:
                    key, value = line.split("=", 1)
                    config[key.strip()] = value.strip()
                else:
                    print(f"Warning: Invalid format on line {line_num}")
                    
    except FileNotFoundError:
        print(f"Config file '{config_file}' not found. Using defaults.")
    
    return config

def write_config(config_file, settings):
    """Write settings to a configuration file."""
    with open(config_file, "w") as file:
        file.write("# Application Configuration\n")
        file.write("# Generated automatically\n\n")
        
        for key, value in settings.items():
            file.write(f"{key}={value}\n")

# Usage
settings = {
    "database_host": "localhost",
    "database_port": "5432", 
    "debug_mode": "true",
    "max_connections": "100"
}

write_config("app.config", settings)
loaded_settings = read_config("app.config")
print(loaded_settings)

Example 2: Student Gradebook

python

import csv

class Gradebook:
    def __init__(self, filename):
        self.filename = filename
        self.students = []
    
    def load_grades(self):
        """Load student grades from CSV file."""
        try:
            with open(self.filename, "r", newline="") as file:
                reader = csv.DictReader(file)
                self.students = list(reader)
            print(f"Loaded {len(self.students)} student records")
        except FileNotFoundError:
            print("Grade file not found. Starting with empty gradebook.")
    
    def save_grades(self):
        """Save student grades to CSV file."""
        if not self.students:
            print("No data to save")
            return
        
        with open(self.filename, "w", newline="") as file:
            fieldnames = self.students[0].keys()
            writer = csv.DictWriter(file, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerows(self.students)
        print(f"Saved {len(self.students)} student records to {self.filename}")
    
    def add_student(self, name, math, science, english):
        """Add a new student with grades."""
        student = {
            "name": name,
            "math": math,
            "science": science, 
            "english": english
        }
        self.students.append(student)
        print(f"Added student: {name}")
    
    def calculate_averages(self):
        """Calculate and display average grades."""
        if not self.students:
            print("No student data available")
            return
        
        print("\nStudent Averages:")
        print("-" * 30)
        for student in self.students:
            total = sum(float(student[subject]) for subject in ["math", "science", "english"])
            average = total / 3
            print(f"{student['name']:15} {average:6.2f}")

# Usage
gradebook = Gradebook("grades.csv")

# Add some sample students
gradebook.add_student("Alice", "85", "92", "78")
gradebook.add_student("Bob", "76", "88", "91") 
gradebook.add_student("Charlie", "90", "85", "87")

# Save to file
gradebook.save_grades()

# Calculate averages
gradebook.calculate_averages()

Example 3: File Backup Utility

python

import shutil
from datetime import datetime

def create_backup(source_file, backup_dir="backups"):
    """Create a timestamped backup of a file."""
    import os
    
    # Create backup directory if it doesn't exist
    if not os.path.exists(backup_dir):
        os.makedirs(backup_dir)
    
    # Generate backup filename with timestamp
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = os.path.basename(source_file)
    backup_file = os.path.join(backup_dir, f"{timestamp}_{filename}")
    
    try:
        shutil.copy2(source_file, backup_file)
        print(f"Backup created: {backup_file}")
        return True
    except Exception as e:
        print(f"Backup failed: {e}")
        return False

def log_operation(message, log_file="operations.log"):
    """Log an operation to a log file with timestamp."""
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    log_entry = f"[{timestamp}] {message}\n"
    
    with open(log_file, "a") as file:
        file.write(log_entry)

# Usage
log_operation("Application started")
create_backup("important_data.txt")
log_operation("Backup completed successfully")

7. Error Handling in File Operations

python

def safe_file_operations(filename):
    """Demonstrate proper error handling with files."""
    try:
        with open(filename, "r") as file:
            content = file.read()
            print("File read successfully")
            return content
            
    except FileNotFoundError:
        print(f"Error: The file '{filename}' was not found.")
    except PermissionError:
        print(f"Error: Permission denied to read '{filename}'.")
    except UnicodeDecodeError:
        print(f"Error: Could not decode '{filename}'. Try specifying encoding.")
    except Exception as e:
        print(f"Unexpected error: {e}")
    finally:
        print("File operation attempt completed.")

# Usage
safe_file_operations("nonexistent_file.txt")

Key Takeaways:

  1. Always use with statements - they automatically handle file closing

  2. Handle exceptions - files might not exist or be accessible

  3. Choose the right mode - 'r' for reading, 'w' for writing, 'a' for appending

  4. Use csv module for CSV files - it handles edge cases properly

  5. Consider encoding - specify encoding='utf-8' for text files

  6. Close files properly - or use with statements to avoid resource leaks

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?