Python File Handling: A Comprehensive Guide with Examples, Study notes of Computer Science

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

2025/2026

Available from 12/28/2025

chetna-gour
chetna-gour 🇮🇳

8 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python File Handling: Comprehensive
Classroom Notes
Introduction to File Handling
What is a File?
In computing, a file is a named location on a storage device that stores
related information. Think of it like a digital container - similar to how
you store documents in folders on your desk, computers store data in
files on disks. Files preserve data permanently, even after your program
stops running or the computer is turned off.
Why File Handling is Needed in Python
Imagine you're building a student management system. You need to
store student records, grades, and attendance. If you only use variables
in your program, all this data disappears when the program ends! File
handling solves this problem by allowing your program to:
1. Store data permanently for future use
2. Retrieve saved data when needed
3. Share data between different programs
4. Process large datasets that don't fit in memory all at once
5. Create logs to track what happened during program execution
Types of Files
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Python File Handling: A Comprehensive Guide with Examples and more Study notes Computer Science in PDF only on Docsity!

Python File Handling: Comprehensive

Classroom Notes

Introduction to File Handling

What is a File?

In computing, a file is a named location on a storage device that stores

related information. Think of it like a digital container - similar to how

you store documents in folders on your desk, computers store data in

files on disks. Files preserve data permanently, even after your program

stops running or the computer is turned off.

Why File Handling is Needed in Python

Imagine you're building a student management system. You need to

store student records, grades, and attendance. If you only use variables

in your program, all this data disappears when the program ends! File

handling solves this problem by allowing your program to:

1. Store data permanently for future use

2. Retrieve saved data when needed

3. Share data between different programs

4. Process large datasets that don't fit in memory all at once

5. Create logs to track what happened during program execution

Types of Files

Python primarily deals with two types of files:

1. Text Files (.txt, .csv, .py, etc.)

○ Human-readable format

○ Store characters encoded as ASCII or Unicode

○ Can be opened in text editors

○ Example: A poem, a Python script, a CSV data file

2. Binary Files (.jpg, .pdf, .exe, etc.)

○ Non-human readable format

○ Store data in bytes (0s and 1s)

○ Require specific programs to interpret

○ Example: Images, videos, compiled programs

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

● Database systems (storing user information)

● Web applications (saving uploaded files)

● Game development (saving progress and high scores)

● Data analysis (processing CSV/Excel files)

● System administration (reading log files)

● Backup software (copying files and folders)

File Modes in Python

file = open("example.txt", "r+")

_# 6. Write and Read ('w+')

Opens for reading and writing

Overwrites existing file or creates new_

file = open("example.txt", "w+")

_# 7. Append and Read ('a+')

Opens for reading and appending

File pointer at end for writing

Creates file if doesn't exist_

file = open("example.txt", "a+")

Binary File Modes

Add 'b' to any mode for binary files:

python

# Reading binary file file = open("image.jpg", "rb")

# Writing binary file

file = open("data.bin", "wb")

Opening and Closing Files

The open() Function

The open() function is your gateway to file operations. It returns a file

object that you can use to perform various operations.

python

# Basic syntax file_object = open(filename, mode, encoding)

# Practical example file = open("students.txt", "r", encoding="utf-8") _# filename: "students.txt" (string)

mode: "r" (read mode)_

# encoding: "utf-8" (character encoding for text files)

The close() Method

Always close files after you're done! This releases system resources and

ensures all data is properly saved.

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?

1. Memory leaks (wasted system resources)

2. Data might not be saved properly_

# 3. File might remain locked (can't be accessed by other programs)

_# Line 2: File handling is important

Line 3: Practice makes perfect_

# 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:

Python is awesome

File handling is important

Practice makes perfect_

# 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

Create initial file_

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?

The with statement automatically handles file closing, even if errors

occur. It's Python's recommended way to work with files.

python

# TRADITIONAL WAY (Problematic if error occurs) file = open("data.txt", "r") content = file.read() _# What if an error happens here?

File might not get closed!_

file.close()

# MODERN WAY (Recommended) with open("data.txt", "r") as file: content = file.read() _# File automatically closes when block ends

Even if error occurs!

No need to call close() - it's automatic_

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

A file pointer is like a bookmark - it keeps track of where you are in the

file. When you read or write, the pointer moves forward.

python

# Visualizing file pointer movement with open("example.txt", "w") as f: f.write("1234567890") _# 10 characters

Pointer is now at position 10 (end of file)_

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

Returns current position of file pointer (in bytes).

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

Moves file pointer to specific position.

python

_# seek(offset, whence)

whence: 0 (beginning), 1 (current), 2 (end)_

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: