Class 12 COMPUTER SCIENCE PPT Chapter 2 File-Handling-In-Python
Class 12 COMPUTER SCIENCE PPT Chapter 2 File-Handling-In-Python
File handling is a crucial skill for any programmer, allowing your programs to
store and retrieve data permanently. By the end of this presentation, you will
understand how to create, read, write, and manipulate files effectively using
Python programming language.
Introduction to File Handling
What is File Handling? Why Do We Need Files?
The process of working with To store data permanently
files for storing and retrieving beyond program execution
data permanently lifetime
Store data in human-readable form using character encoding like Store data in machine-readable form (0s and 1s). Examples
ASCII or Unicode. Examples include .txt, .csv, .html files. include .jpg, .mp3, .pdf, .exe files.
These files store sequences of characters and can be opened with These files contain raw bytes and require specialized software to
any text editor. Common operations include reading lines of text interpret. They are used for storing non-textual data like images,
and writing strings. videos, and program executables.
File Operations in Python
Close
file.close() - Closes the file and releases resources
Write/Append
file.write() - Adds content to the file
Read
file.read() - Retrieves content from the file
Open
open() - Creates file object and establishes connection
Opening a File - The open() Function
Syntax
file_object = open(filename, mode)
Parameters
filename (path) and mode (how to open)
Return Value
Returns a file object for operations
The open() function is the gateway to file handling in Python. It establishes a connection between your program and the file system,
creating a file object that serves as a handle for all subsequent operations on that file.
File Opening Modes
Text Modes Binary Modes
• 'r' - Read (default mode) • 'rb' - Read binary
• 'w' - Write (creates • 'wb' - Write binary
new/truncates) • 'ab' - Append binary
• 'a' - Append (adds to end) • 'xb' - Exclusive binary creation
• 'x' - Exclusive creation
Special Modes
• 'r+' - Read and write
• 'w+' - Write and read (truncates)
• 'a+' - Append and read
• 't' - Text mode (default)
Reading Files - Complete File
Open the file in read mode
file = open("example.txt", "r")
# Opening a file and reading all content In the first example, we open the file in read mode, read all its
file = open("sample.txt", "r") content at once, print it, and then close the file.
content = file.read()
print(content) In the second example, we iterate through the file line by line using a
file.close()
for loop, which is more memory-efficient for large files as it doesn't
# Reading line by line load the entire content into memory at once.
file = open("sample.txt", "r")
for line in file:
print(line, end='')
file.close()
The with Statement - Context
Manager
Traditional Approach with Statement
• Explicitly open and close files • Automatically manages
• Must handle exceptions resources
Syntax
• with open(filename, mode) as file:
• # File operations
• # File is automatically closed here
Example: Using with Statement
Code Example Advantages
# Using with statement for file handling The with statement creates a context where the file is
with open("example.txt", "r") as file: automatically closed when the block ends, even if exceptions
content = file.read() occur.
print(content)
# File is automatically closed here
This approach is cleaner, safer, and recommended as a best
# Reading line by line with with practice for file handling in Python. It eliminates the need for
statement explicit file.close() calls and reduces the risk of resource leaks.
with open("example.txt", "r") as file:
for line in file:
print(line, end='')
Writing to Text Files
Open file in write mode
file = open("new_file.txt", "w")
Syntax
file = open("filename.txt", "a")
Use Case
Logs, continuous data collection, adding records
The append mode is particularly useful when you want to add new information
to a file without disrupting its existing content. This is commonly used in
applications such as log files, where new entries need to be continually added to
the end of the file.
Example: Appending to a File
Append Mode Example Result in log.txt
When writing to files, it's important to remember that the write() and writelines() methods do not automatically add newline characters. You
must include '\n' in your strings if you want line breaks in your file. The print() function, however, does add newlines by default.
File Positions and Seeking
File Position Seeking Methods
Every open file has a current position indicator that determines • tell() - Returns current position
where the next read or write operation will occur. This position • seek(offset, whence) - Moves position
automatically advances after each operation.
• whence: 0 (start), 1 (current), 2 (end)
• Initially at the beginning (for 'r' and 'w') • seek(0) - Move to beginning
• Initially at the end (for 'a') • seek(0, 2) - Move to end
Example: File Seeking
Open file for reading and writing
with open("sample.txt", "r+") as file:
Move to beginning
file.seek(0) # Reset to beginning
Working with Binary Files
Opening Binary Files Reading Binary Files Writing Binary Files
• Use 'b' with mode: 'rb', 'wb', 'ab' • read() returns bytes objects • write() takes bytes objects
• Handles data as bytes, not • No line concept - just bytes • Convert strings: bytes(str,
characters • Common for images, audio, video encoding)
• No encoding/decoding of text • Or use str.encode(encoding)
Example: Binary File Handling
Binary File Copy Example Text vs Binary
# Copying an image file When dealing with binary files like images, audio, or video, we
with open("source.jpg", "rb") as source: must use binary mode to ensure the data is not corrupted during
with open("destination.jpg", "wb") as processing.
dest:
# Read and write in chunks
The example shows a common pattern for copying large files
chunk_size = 4096 # 4KB chunks
while True: efficiently by reading and writing in manageable chunks rather
chunk = than loading the entire file into memory at once.
source.read(chunk_size)
if not chunk:
break
dest.write(chunk)
File Encoding
Encoding Errors
Specifying Encoding
UnicodeDecodeError,
UnicodeEncodeError
3 open(file, mode, encoding="utf-8")
Example: Working with Encodings
Code Example Importance of Encoding
# Writing text with specific encoding UTF-8 is the most common encoding that can represent virtually any
with open("unicode.txt", "w", character from any language. When working with international text, always
encoding="utf-8") as file: specify encoding to avoid corruption.
file.write("Hello in English\n")
file.write("Здравствуйте на русском\n")
If you try to open a UTF-8 file with a different encoding (e.g., ASCII), you'll get
file.write("हिंदी में नमस्ते\n")
file.write(" 日本語でこんにちは \n") errors when encountering non-ASCII characters.
CSV (Comma-Separated Values) is a simple format for storing tabular data where
each value is separated by commas and each record by newlines. Python's csv
module provides specialized tools for reading and writing CSV files, handling
complications like commas within fields and different delimiters.
Example: CSV File Handling
Writing to CSV Reading from CSV
Try-Except Blocks
Catch and handle exceptions gracefully
Finally Clause
Ensure resources are properly released
Best Practice
Use 'with' statement to auto-handle resources
Example: Error Handling
Traditional Try-Except With Context Manager
# Error handling with traditional approach # Error handling with 'with' statement
try: try:
file = open("nonexistent.txt", "r") with open("nonexistent.txt", "r") as file:
content = file.read() content = file.read()
print(content) print(content)
except FileNotFoundError: except FileNotFoundError:
print("File does not exist!") print("File does not exist!")
except PermissionError: except PermissionError:
print("No permission to read file!") print("No permission to read file!")
finally: # No need for finally - file auto-closes
if 'file' in locals() and not file.closed:
file.close()
print("File closed in finally block")
Working with Directories
os Module Functions
Python's os module provides functions for working with directories and
file paths
Creating Directories
os.mkdir() for single directory, os.makedirs() for nested path
Removing Directories
os.rmdir() for empty directory, shutil.rmtree() for non-empty
Example: Directory Operations
Working with Directories Path Operations
Path Operations
• os.path - Traditional module
• pathlib - Modern object-oriented module
• Platform-independent operations
• Path manipulation and properties
Example: Path Handling with pathlib
Using pathlib Module Path Operations
Security Note
Never unpickle data
from untrusted
sources
Example: Using Pickle Module
Pickling Objects Unpickling Objects
Advantages
Human-readable, language-independent, widely supported
Python Support
Built-in json module for encoding and decoding
Common Uses
Web APIs, configuration files, data storage
Example: JSON File Handling
Writing JSON Data Reading JSON Data
Data Processing
Manipulating and analyzing the data using Python's data analysis
libraries (pandas, numpy)
Result Output
Writing processed results back to files in appropriate formats for
sharing or further use
Example: Data Analysis with File I/O
Data Analysis Code Applications
Data analysis often involves reading data from files, processing it to extract meaningful insights, and then writing
import csv
from statistics import mean, median, mode the results to new files for reporting or visualization.
# Read temperature data from CSV This example demonstrates a simple workflow for analyzing temperature data from a CSV file, calculating basic
temperatures = [] statistics, and writing a formatted report to a text file.
with open("weather.csv", "r") as file:
reader = csv.DictReader(file)
For more complex analysis, libraries like pandas provide powerful tools for working with structured data from
for row in reader:
temperatures.append(float(row["temperature"])) various file formats.
# Calculate statistics
avg_temp = mean(temperatures)
median_temp = median(temperatures)
max_temp = max(temperatures)
min_temp = min(temperatures)
Permissions Management
• Use minimal required permissions
• Check file permissions before operations
• Handle sensitive data with care
Testing File Operations
Create test fixtures
Sample files for testing in controlled environment
Files created for short-term use that are automatically deleted Python's standard library provides the tempfile module with
when they're no longer needed. They're useful for: functions to create secure temporary files and directories:
Python Modules
zipfile, gzip, bz2, and tarfile for various compression formats
Common Formats
ZIP, GZIP, BZIP2, and TAR for different compression needs
Tradeoffs
Balance between compression ratio, speed, and compatibility
Example: Working with ZIP Files
Creating a ZIP Archive Extracting Files from ZIP
Generator-Based Reading
Use generators to yield lines or chunks one at a time, keeping memory
usage constant
Memory-Mapped Files
Use mmap module to map file content directly to memory for
efficient random access
Example: Processing Large Files
Chunk-Based Processing Line Generator for Text Files
Shared Memory
Multiple processes can
share mapped memory
Example: Memory-Mapped File
Using mmap Module Benefits and Considerations
import mmap Memory-mapped files provide a performance boost when working with large files, especially for random access patterns. They're commonly used in:
import os
# Reset position
mm.seek(0)
if position != -1:
mm.seek(position)
# Read 20 bytes from the pattern position
print(f"\nFound at position {position}:")
print(mm.read(20).decode())
Remote Protocols
FTP, SFTP, HTTP/HTTPS for remote files
# Example usage
upload_file_ftp('ftp.example.com', 'user', 'pass',
'report.pdf', '/uploads')
File Monitoring and Watching
File Change Detection
Identify when files are modified, created, or deleted
Real-time Processing
Take actions immediately when files change
Python Libraries
watchdog, pyinotify, and polling techniques
Applications
Log monitoring, auto-reload, and synchronization
Example: File Monitoring with watchdog
File Change Monitor Applications
if __name__ == "__main__":
# Path to monitor
path = "." # Current directory
# Initialize Observer
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
try:
while True:
# Keep the script running
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
File System Permissions and
Access Control
File Permissions Checking Permissions
• Read, write, and execute • os.access() - Check user
permissions permissions
• Owner, group, and other user • os.stat() - Get detailed file status
categories • Platform-specific permission
• Platform-specific implementation masks
Modifying Permissions
• os.chmod() - Change permissions
• Numeric (octal) or symbolic modes
• Consider security implications
Example: Working with File Permissions
Checking and Setting Permissions Setting Permissions
return result
Handling Special File Types
PDF Files
Using PyPDF2 or
reportlab libraries
Example: Working with Image Files
Image Processing with Pillow Working with Excel Files
# Install Pillow first: pip install Pillow # Install pandas and openpyxl first:
from PIL import Image, ImageFilter # pip install pandas openpyxl
import pandas as pd
def process_image(input_path, output_path,
size=(300, 300)): def excel_file_operations():
"""Resize and filter an image file.""" """Demonstrate pandas Excel operations."""
try: # Create sample data
# Open the image data = {
with Image.open(input_path) as img: 'Name': ['Rahul', 'Priya', 'Amit', 'Ananya'],
# Display original details 'Age': [17, 18, 16, 17],
print(f"Original image: {input_path}") 'Marks': [88, 92, 75, 95]
print(f"Size: {img.size}") }
print(f"Format: {img.format}")
print(f"Mode: {img.mode}") # Create DataFrame
df = pd.DataFrame(data)
# Resize the image
resized_img = img.resize(size) # Write to Excel
df.to_excel("students.xlsx", sheet_name="Students",
# Apply a filter (e.g., sharpen) index=False)
filtered_img = resized_img.filter( print("Data written to Excel file")
ImageFilter.SHARPEN)
# Read from Excel
# Save the result read_df = pd.read_excel("students.xlsx")
filtered_img.save(output_path) print("\nData read from Excel file:")
print(f"Processed image saved to: {output_path}") print(read_df)
return True
# Calculate statistics
except Exception as e: avg_mark = read_df['Marks'].mean()
print(f"Error processing image: {e}") max_mark = read_df['Marks'].max()
return False
print(f"\nAverage Mark: {avg_mark:.2f}")
# Example usage print(f"Maximum Mark: {max_mark}")
process_image("input.jpg", "output_resized.jpg")
# Add new data
read_df = read_df.append({'Name': 'Vikram',
'Age': 18,
'Marks': 85},
ignore_index=True)
Advanced Operations
CSV, JSON, compression, and specialized file types
4 Best Practices
Error handling, security, and efficiency considerations
File handling is a fundamental skill for any Python programmer. It allows you to work with persistent data, process information from various sources, and create applications that can store and retrieve information
reliably. By mastering the concepts covered in this presentation, you're well-equipped to handle file operations effectively in your programming projects.