

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive guide to file handling in python, covering various modes, methods, and best practices. It includes practical examples for reading, writing, and appending files, as well as handling large files efficiently. The guide also addresses safe file handling practices, error handling, and real-world applications such as database systems, web applications, and log file analysis. It is designed to help users understand and implement file handling techniques effectively in their python projects, ensuring data integrity and efficient resource management. The document also includes examples of student record management systems and csv data processing.
Typology: Study notes
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























python
# Simple analogy to understand the difference text_content = "Hello World" # Text: You can read it directly
binary_content = b'\x48\x65\x6c\x6c\x6f' # Binary: Machine format
Real-Life Applications of File Handling
File Modes in Python
file = open("example.txt", "r+")
_# 6. Write and Read ('w+')
file = open("example.txt", "w+")
_# 7. Append and Read ('a+')
file = open("example.txt", "a+")
Binary File Modes
python
# Reading binary file file = open("image.jpg", "rb")
# Writing binary file
file = open("data.bin", "wb")
Opening and Closing Files
The open() Function
python
# Basic syntax file_object = open(filename, mode, encoding)
# Practical example file = open("students.txt", "r", encoding="utf-8") _# filename: "students.txt" (string)
# encoding: "utf-8" (character encoding for text files)
The close() Method
python
# Opening and closing a file - CORRECT WAY file = open("data.txt", "r") content = file.read() print(content) file.close() # Always close the file!
_# What happens if you don't close?
# 3. File might remain locked (can't be accessed by other programs)
_# Line 2: File handling is important
# Method 1: read() - Reads entire file file = open("sample.txt", "r") content = file.read() # Reads everything print("Method 1 - read():") print(content) file.close()
_# Output:
# Method 2: read(size) - Reads specific number of characters file = open("sample.txt", "r") part1 = file.read( 10 ) # Reads first 10 characters print("\nMethod 2 - read(10):") print(part1) # Output: Python is file.close()
# Method 3: readline() - Reads one line at a time file = open("sample.txt", "r") line1 = file.readline() # Reads first line line2 = file.readline() # Reads second line print("\nMethod 3 - readline():") print("Line 1:", line1.strip()) # strip() removes newline character print("Line 2:", line2.strip()) file.close()
# Method 4: readlines() - Reads all lines into a list file = open("sample.txt", "r") lines = file.readlines() # Returns list of lines print("\nMethod 4 - readlines():") for line in lines: print(f"Line: {line.strip()}") file.close()
Reading Files Using Loops
python
# Most efficient way to read large files file = open("sample.txt", "r")
print("Reading using for loop:") for line in file: # Reads line by line without loading entire file print(f"→ {line.strip()}")
file.close()
# Alternative with line number file = open("sample.txt", "r") print("\nReading with line numbers:") for i, line in enumerate(file, 1 ): print(f"Line {i}: {line.strip()}") file.close()
Handling Large Files Efficiently
python
# For very large files (GBs), read in chunks def read_large_file(filename, chunk_size= 1024 ): """Read large file in chunks to save memory""" with open(filename, "r") as file: while True: chunk = file.read(chunk_size) # Read 1KB at a time if not chunk: # Empty string means EOF break # Process chunk here print(chunk, end="")
# Example usage
read_large_file("big_data.txt")
Writing and Appending to Files
# Day 2: Started file handling
# Day 3: Working on projects
Important Difference: Write vs Append
python
_# DEMONSTRATION
with open("demo.txt", "w") as f: f.write("Original content\n")
# Append mode - ADDS to existing with open("demo.txt", "a") as f: f.write("Appended line 1\n") f.write("Appended line 2\n")
# Write mode - OVERWRITES everything with open("demo.txt", "w") as f: f.write("New content - old is gone!\n")
# Final content: Only "New content - old is gone!"
Writing Lists and Dictionaries to Files
python
# Writing a list to file student_names = ["Alice", "Bob", "Charlie", "Diana"]
with open("students.txt", "w") as file: for student in student_names: file.write(student + "\n") # Add newline manually
# Writing dictionary data student_data = { "name": "John Doe",
"age": 20 , "grade": "A", "courses": ["Math", "Science"] }
with open("student_record.txt", "w") as file: for key, value in student_data.items(): if isinstance(value, list): value = ", ".join(value) file.write(f"{key}: {value}\n")
With Statement (Context Manager)
Why Use with Statement?
python
# TRADITIONAL WAY (Problematic if error occurs) file = open("data.txt", "r") content = file.read() _# What if an error happens here?
file.close()
# MODERN WAY (Recommended) with open("data.txt", "r") as file: content = file.read() _# File automatically closes when block ends
words = content.split() return len(words) except FileNotFoundError: print(f"Error: {filename} not found!") return 0 except IOError as e: print(f"IO Error: {e}") return 0
# Usage word_count = count_words_in_file("article.txt")
print(f"Total words: {word_count}")
File Pointer & Seeking
Understanding File Pointer
python
# Visualizing file pointer movement with open("example.txt", "w") as f: f.write("1234567890") _# 10 characters
with open("example.txt", "r") as f: print(f"Initial position: {f.tell()}") # Position: 0 chunk = f.read( 3 ) # Reads "123" print(f"After reading 3 chars: {f.tell()}") # Position: 3 chunk = f.read( 2 ) # Reads "45"
print(f"After reading 2 more: {f.tell()}") # Position: 5
The tell() Method
python
with open("test.txt", "r") as file: print(f"Start: {file.tell()}") file.read( 5 ) print(f"After 5 chars: {file.tell()}") file.readline() print(f"After line: {file.tell()}")
The seek() Method
python
_# seek(offset, whence)
with open("data.txt", "r") as file: # Write some content first with open("data.txt", "w") as wfile: wfile.write("Line 1: Python\nLine 2: Java\nLine 3: C++\n") # Now read with seeking file.seek( 0 ) # Go to beginning print("Start:", file.readline()) # Line 1
# 1. FileNotFoundError try: file = open("nonexistent.txt", "r") except FileNotFoundError as e: print(f"Error: {e}") # File not found
# 2. PermissionError try: file = open("/root/system.txt", "w") # On Unix: needs root except PermissionError as e: print(f"Permission denied: {e}")
# 3. IsADirectoryError try: file = open("/home", "r") # /home is a directory! except IsADirectoryError: print("Cannot open directory as file")
# 4. IOError (General I/O error) try: file = open("file.txt", "r") # Disk error occurs while reading except IOError as e:
print(f"I/O Error: {e}")
Safe File Handling Practices
python
def safe_file_operation(filename, mode="r"): """Safely perform file operations with error handling""" try: with open(filename, mode, encoding="utf-8") as file: if "r" in mode: return file.read() elif "w" in mode or "a" in mode: # For writing operations return file except FileNotFoundError: print(f"Error: '{filename}' not found.") return None
except PermissionError: print(f"Error: No permission to access '{filename}'.") return None except IOError as e: print(f"IO Error: {e}") return None except UnicodeDecodeError: print(f"Error: Cannot decode file '{filename}' as UTF-8.") return None
# Usage content = safe_file_operation("important.txt") if content:
print("File read successfully!")
Complete Error Handling Example
python
def backup_file(source, destination): """Create backup with comprehensive error handling""" try: # Check if source exists with open(source, "r") as src: data = src.read() # Check if destination is writable with open(destination, "w") as dest: dest.write(data) print(f"Backup created: {source} → {destination}") return True except FileNotFoundError: print(f"Error: Source file '{source}' not found.") return False except PermissionError: print(f"Error: Cannot write to '{destination}'.") return False except IOError as e: print(f"IO Error during backup: {e}")
print(f"Average words per line: {len(words)/max( 1 , len(non_empty_lines)):.2f}")
# Most common words (simplified) word_count = {} for word in words: word = word.lower().strip('.,!?;:"') if word: word_count[word] = word_count.get(word, 0 ) + 1 # Get top 5 words top_words = sorted(word_count.items(), key=lambda x: x[ 1 ], reverse=True)[: 5 ] print("\nTop 5 frequent words:") for word, count in top_words: print(f" {word}: {count} times") except FileNotFoundError: print(f"Error: File '{filename}' not found.")
# Usage
analyze_text_file("sample.txt")
Example 2: Student Records Management System
python
class StudentRecordSystem: """Simple student record system using files""" def init(self, filename="students.db"): self.filename = filename self.ensure_file_exists() def ensure_file_exists(self): """Create file if it doesn't exist""" try: open(self.filename, "r").close() except FileNotFoundError: with open(self.filename, "w") as f: f.write("ID,Name,Age,Grade\n")
def add_student(self, student_id, name, age, grade): """Add a new student record""" with open(self.filename, "a") as file: file.write(f"{student_id},{name},{age},{grade}\n") print(f"Student {name} added successfully.") def view_all_students(self): """Display all student records""" try: with open(self.filename, "r") as file: header = file.readline().strip().split(',') print(f"\n{' | '.join(header)}") print("-" * 40 ) for line in file: if line.strip(): data = line.strip().split(',') print(f"{data[ 0 ]: 3 } | {data[ 1 ]: 15 } | {data[ 2 ]: 3 } | {data[ 3 ]: 5 }") except FileNotFoundError: print("No records found.") def search_student(self, search_term): """Search for student by ID or name""" found = False with open(self.filename, "r") as file: file.readline() # Skip header for line in file: if line.strip(): data = line.strip().split(',') if search_term in data: # Check ID or name print(f"\nStudent Found:") print(f"ID: {data[ 0 ]}") print(f"Name: {data[ 1 ]}") print(f"Age: {data[ 2 ]}") print(f"Grade: {data[ 3 ]}") found = True if not found: print(f"No student found with '{search_term}'") def delete_student(self, student_id): """Delete a student record""" try: