Virtual assistance

Python File Handling

Learn how to read from and write to files in Python. Master file operations, different file modes, and best practices for file handling.

Python File Handling

What is File Handling?

File handling involves reading data from files and writing data to files. Python provides built-in functions and methods to work with files efficiently and safely.

"File handling enables persistent data storage and retrieval in Python programs."

Opening and Closing Files

Use the open() function to work with files:

# Opening a file
file = open('example.txt', 'r')  # 'r' for read mode

# Always close files
file.close()

# Better: Use with statement (automatically closes)
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)
# File is automatically closed here

File Modes

Different modes for opening files:

ModeDescription
'r'Read (default) - File must exist
'w'Write - Creates new file or truncates existing
'a'Append - Adds to end of file
'x'Exclusive creation - Fails if file exists
'r+'Read and write
'w+'Read and write (truncates)
'a+'Read and append
'b'Binary mode
't'Text mode (default)

Reading Files

Various ways to read file content:

# Read entire file
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

# Read line by line
with open('example.txt', 'r') as file:
    for line in file:
        print(line.strip())  # strip() removes newline

# Read all lines into a list
with open('example.txt', 'r') as file:
    lines = file.readlines()
    print(lines)

# Read specific number of characters
with open('example.txt', 'r') as file:
    chunk = file.read(100)  # Read first 100 characters
    print(chunk)

# Read one line at a time
with open('example.txt', 'r') as file:
    line1 = file.readline()
    line2 = file.readline()
    print(line1, line2)

Writing Files

Writing data to files:

# Write to a new file
with open('output.txt', 'w') as file:
    file.write("Hello, World!\n")
    file.write("This is a new file.\n")

# Append to existing file
with open('output.txt', 'a') as file:
    file.write("This line is appended.\n")

# Write multiple lines
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open('output.txt', 'w') as file:
    file.writelines(lines)

# Using print() with file parameter
with open('output.txt', 'w') as file:
    print("Hello", "World", sep=", ", file=file)
    print("Python is awesome!", file=file)

File Position and Seeking

Control file pointer position:

with open('example.txt', 'r') as file:
    print(file.tell())  # Current position: 0
    
    content = file.read(10)
    print(file.tell())  # Position after reading 10 chars
    
    file.seek(0)  # Go back to beginning
    print(file.tell())  # Position: 0
    
    file.seek(5)  # Go to position 5
    content = file.read(5)
    print(content)  # Characters 5-9

Working with Binary Files

Handle binary data like images:

# Read binary file
with open('image.jpg', 'rb') as file:
    data = file.read()
    print(f"Read {len(data)} bytes")

# Write binary file
with open('copy.jpg', 'wb') as file:
    file.write(data)

# Copy file efficiently
with open('source.jpg', 'rb') as source:
    with open('destination.jpg', 'wb') as dest:
        while chunk := source.read(4096):  # Read in chunks
            dest.write(chunk)

File and Directory Operations

Use os and shutil modules:

import os
import shutil

# Check if file exists
if os.path.exists('example.txt'):
    print("File exists")

# Get file information
print(os.path.getsize('example.txt'))  # File size in bytes
print(os.path.getmtime('example.txt')) # Last modification time

# Rename file
os.rename('old_name.txt', 'new_name.txt')

# Remove file
if os.path.exists('temp.txt'):
    os.remove('temp.txt')

# Create directory
os.makedirs('new_directory', exist_ok=True)

# List directory contents
files = os.listdir('.')
print(files)

# Copy file
shutil.copy('source.txt', 'destination.txt')

# Move file
shutil.move('file.txt', 'new_location/file.txt')

# Remove directory
shutil.rmtree('directory_to_remove')

Working with CSV Files

Use csv module for structured data:

import csv

# Writing CSV
data = [
    ['Name', 'Age', 'City'],
    ['Alice', 25, 'New York'],
    ['Bob', 30, 'London'],
    ['Charlie', 35, 'Paris']
]

with open('people.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data)

# Reading CSV
with open('people.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

# Using DictReader/DictWriter
with open('people.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(f"{row['Name']} is {row['Age']} years old")

Context Managers

Create custom context managers:

class FileManager:
    def __init__(self, filename, mode):
        self.filename = filename
        self.mode = mode
        self.file = None
    
    def __enter__(self):
        self.file = open(self.filename, self.mode)
        return self.file
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.file:
            self.file.close()

# Using custom context manager
with FileManager('example.txt', 'r') as file:
    content = file.read()
    print(content)

# Using contextlib
from contextlib import contextmanager

@contextmanager
def file_manager(filename, mode):
    file = open(filename, mode)
    try:
        yield file
    finally:
        file.close()

with file_manager('example.txt', 'r') as file:
    content = file.read()

File Handling Best Practices

  • Use with statement: Ensures files are properly closed
  • Handle exceptions: File operations can fail
  • Specify encoding: For text files (UTF-8 recommended)
  • Use absolute paths: When necessary
  • Check file existence: Before operations
  • Use appropriate modes: Choose correct read/write mode
  • Read in chunks: For large files
  • Backup important files: Before modification

Common File Handling Patterns

# Safe file reading with error handling
def read_file_safely(filename):
    try:
        with open(filename, 'r', encoding='utf-8') as file:
            return file.read()
    except FileNotFoundError:
        print(f"File {filename} not found")
        return None
    except PermissionError:
        print(f"No permission to read {filename}")
        return None
    except UnicodeDecodeError:
        print(f"Encoding error in {filename}")
        return None

# Configuration file reader
def load_config(filename):
    config = {}
    with open(filename, 'r') as file:
        for line in file:
            if '=' in line:
                key, value = line.strip().split('=', 1)
                config[key] = value
    return config

# Log file writer
import datetime

def write_log(message, filename='app.log'):
    timestamp = datetime.datetime.now().isoformat()
    with open(filename, 'a') as file:
        file.write(f"[{timestamp}] {message}\n")

# Temporary file creation
import tempfile

with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp:
    temp.write("Temporary data")
    temp_path = temp.name

# Process temp file
with open(temp_path, 'r') as file:
    print(file.read())

# Clean up
os.unlink(temp_path)

File handling is essential for data persistence in Python applications. Always use proper error handling and the with statement to ensure files are managed correctly.