0% found this document useful (0 votes)
22 views

Class 12 COMPUTER SCIENCE PPT Chapter 2 File-Handling-In-Python

This document provides a comprehensive overview of file handling in Python, covering essential concepts such as file operations, types of files, and practical applications for Class 12 Computer Science students. It explains how to create, read, write, and manipulate both text and binary files, along with error handling and directory operations. Additionally, it introduces the use of the 'with' statement for resource management and discusses CSV file handling and path operations.

Uploaded by

Susan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Class 12 COMPUTER SCIENCE PPT Chapter 2 File-Handling-In-Python

This document provides a comprehensive overview of file handling in Python, covering essential concepts such as file operations, types of files, and practical applications for Class 12 Computer Science students. It explains how to create, read, write, and manipulate both text and binary files, along with error handling and directory operations. Additionally, it introduces the use of the 'with' statement for resource management and discusses CSV file handling and path operations.

Uploaded by

Susan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

File Handling in Python

Welcome to our presentation on File Handling in Python, an essential topic for


Class 12 Computer Science students. Throughout this presentation, we will
explore the fundamentals of file operations, different file modes, and practical
applications according to the NCERT curriculum.

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

Benefits of File Handling


Data persistence, sharing data between programs, and maintaining
records
Types of Files in Python
Text Files Binary Files

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")

Read entire content


content = file.read()

Process the content


print(content) # Or any other processing

Close the file


file.close()
Reading Files - Line by Line

Open File Read Lines


file = open("data.txt", "r") Use readline() or for loop

Close File Process Each Line


file.close() Work with line content
File Reading Methods
Method Description Example

read() Reads entire file content = f.read()


content

read(size) Reads specified chunk = f.read(10)


number of
characters

readline() Reads a single line line = f.readline()

readlines() Returns all lines as a lines = f.readlines()


list
Example: Reading a File
Code Example Explanation

# 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

• • Handles exceptions gracefully


Must remember to close files
• Automatically closes files

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")

Write content to file


file.write("Hello, Python!")

Write multiple lines


file.write("\nMultiple\nLines")

Close the file


file.close()
Example: Writing to a File
Write Mode Example Output in output.txt

# Opening a file in write mode Hello, this is line 1


with open("output.txt", "w") as file: This is line 2
file.write("Hello, this is line 1\n") Line 3
file.write("This is line 2\n") Line 4
Line 5
# Writing a list of lines
lines = ["Line 3\n", "Line 4\n",
"Line 5\n"]
file.writelines(lines)

The write() method writes a string to the file. To write multiple


lines, we either include newline characters (\n) or use the
writelines() method with a list of strings.
Note: Write mode ("w") creates a new file or truncates an existing
file, erasing its previous content.
Appending to Files
Append Mode
Opens file for adding content without erasing existing data

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

# First, create a file with initial Original entry: 2023-08-01


content New entry: 2023-08-02
with open("log.txt", "w") as file: Another entry: 2023-08-03
file.write("Original entry: 2023-08-
01\n")

# Later, append more content


with open("log.txt", "a") as file: Using append mode preserves all existing content in the file and
file.write("New entry: 2023-08-02\n")
file.write("Another entry: 2023-08- adds new content at the end. This is perfect for logs, journals, or
03\n") any situation where historical data needs to be maintained.
File Writing Methods
write(string) writelines(list) print(content,
file=file_object)
Writes the string content to the file Writes a list of strings to the file
at the current position without adding newline characters Alternative way to write to a file
using the print function

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:

Read first 5 characters


data = file.read(5) # Position now at 5

Check current position


print(file.tell()) # Output: 5

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

Character Encoding Common Encodings


Maps characters to binary values UTF-8, ASCII, Latin-1

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.

# Reading with the same encoding


with open("unicode.txt", "r",
encoding="utf-8") as file:
content = file.read()
print(content)
CSV File Handling

What is CSV? CSV Module Common Uses


Comma-Separated Python's built-in Data exchange,
Values format for module for CSV spreadsheets,
tabular data processing databases

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

import csv import csv

# Writing to a CSV file # Reading from a CSV file


with open('data.csv', 'w', newline='') as with open('data.csv', 'r') as file:
file: reader = csv.reader(file)
writer = csv.writer(file) header = next(reader) # Get header
writer.writerow(['Name', 'Age', row
'City']) print("Header:", header)
writer.writerow(['Alice', 25, for row in reader:
'Mumbai']) print(f"Name: {row[0]}, Age:
writer.writerow(['Bob', 30, 'Delhi']) {row[1]}")
writer.writerow(['Charlie', 22,
'Kolkata'])
Error Handling in File
Operations
Common File Errors
FileNotFoundError, PermissionError, IOError

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

Listing Directory Contents


os.listdir() or os.scandir() for more detailed info

Removing Directories
os.rmdir() for empty directory, shutil.rmtree() for non-empty
Example: Directory Operations
Working with Directories Path Operations

import os import os.path


import shutil
# Join path components
# Get current working directory file_path = os.path.join("folder", "subfolder",
cwd = os.getcwd() "file.txt")
print(f"Current directory: {cwd}") print(file_path) # OS-appropriate path

# Create a new directory # Check if path exists


os.mkdir("new_folder") if os.path.exists(file_path):
print("Path exists")
# List directory contents else:
contents = os.listdir(".") print("Path does not exist")
print(f"Directory contents: {contents}")
# Split path into components
# Create nested directories dir_name, file_name = os.path.split(file_path)
os.makedirs("parent/child/grandchild") print(f"Directory: {dir_name}")
print(f"File: {file_name}")
# Remove directory (if empty)
os.rmdir("new_folder")

# Remove directory tree


shutil.rmtree("parent")
File Path Handling
Absolute Paths Relative Paths
• Complete path from root • Path relative to current directory
directory • folder/file.txt
• Windows: C:\folder\file.txt • ../sibling_folder/file.txt
• Unix/Linux: /home/user/file.txt • Location depends on current
• Always points to same location directory

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

from pathlib import Path from pathlib import Path


import time
# Create Path object
p = Path("documents/report.txt") p = Path("example.txt")

# Path properties # Check existence


print(p.name) # report.txt if p.exists():
print(p.stem) # report print(f"File exists: {p}")
print(p.suffix) # .txt
print(p.parent) # documents # File properties
print(f"Is file: {p.is_file()}")
# Joining paths print(f"Is directory: {p.is_dir()}")
new_path = p.parent / "data" / "records.csv"
print(new_path) # documents/data/records.csv # File size
print(f"Size: {p.stat().st_size} bytes")
# Home directory
home = Path.home() # Modification time
print(home) # /home/user or C:\Users\user mtime = time.ctime(p.stat().st_mtime)
print(f"Modified: {mtime}")
Pickle Module - Object
Serialization
What is Pickle? Serialization Deserialization
Python module for Converting Python Converting byte
serializing and objects to byte streams back to
deserializing objects streams for storage Python objects

Security Note
Never unpickle data
from untrusted
sources
Example: Using Pickle Module
Pickling Objects Unpickling Objects

import pickle import pickle

# Sample data structures # Read from a pickle file


data = { with open('data.pkl', 'rb') as file:
'name': 'Student Records', loaded_data = pickle.load(file)
'students': [
{'id': 1, 'name': 'Rahul', 'grade': 'A'}, # Access the reconstructed object
{'id': 2, 'name': 'Priya', 'grade': 'A+'}, print(f"School: {loaded_data['school']}")
{'id': 3, 'name': 'Amit', 'grade': 'B'} print("Students:")
], for student in loaded_data['students']:
'school': 'Delhi Public School' print(f" - {student['name']}: {student['grade']}")
}

# Write to a pickle file


with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
print("Data successfully pickled")
JSON File Handling
What is JSON?
JavaScript Object Notation - lightweight data interchange format

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

import json import json

# Sample data # Read from JSON file


student_data = { with open("students.json", "r") as file:
"students": [ loaded_data = json.load(file)
{
"name": "Aarav Kumar", # Access the data
"age": 17, for student in loaded_data["students"]:
"subjects": ["Physics", "Chemistry", name = student["name"]
"Mathematics"], avg_marks = sum(student["marks"].values()) / len(student["marks"])
"marks": { print(f"{name}: Average marks = {avg_marks:.1f}")
"Physics": 92,
"Chemistry": 88, # Print subjects
"Mathematics": 95 print(f" Subjects: {', '.join(student['subjects'])}")
}
}, # Print individual marks
{ for subject, mark in student["marks"].items():
"name": "Sneha Sharma", print(f" - {subject}: {mark}")
"age": 16, print()
"subjects": ["Biology", "Chemistry",
"Physics"],
"marks": {
"Biology": 94,
"Chemistry": 89,
"Physics": 85
}
}
]
}

# Write to JSON file


with open("students.json", "w") as file:
json.dump(student_data, file, indent=4)
File Operations for Data
Analysis
Data Collection
Reading data from various file sources like CSV, JSON, or text files into
program memory

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)

# Write results to a file


with open("temp_analysis.txt", "w") as file:
file.write("Temperature Analysis\n")
file.write("===================\n\n")
file.write(f"Average: {avg_temp:.2f}°C\n")
file.write(f"Median: {median_temp:.2f}°C\n")
file.write(f"Maximum: {max_temp:.2f}°C\n")
file.write(f"Minimum: {min_temp:.2f}°C\n")
File-Based Database Applications

Data Storage Data Retrieval


1
Files as persistent storage Finding and reading specific records

Data Addition Data Modification


Appending new records Updating existing records
Example: Student Record System
Functions Main Program

def add_student(roll_no, name, marks): # Main program


with open("students.txt", "a") as file: while True:
file.write(f"{roll_no},{name},{marks}\n") print("\nStudent Record System")
print(f"Added student: {name}") print("1. Add Student")
print("2. Display All Students")
def display_all_students(): print("3. Search Student")
try: print("4. Exit")
with open("students.txt", "r") as file:
print("\nStudent Records:") choice = input("Enter your choice (1-4): ")
print("Roll No | Name | Marks")
print("-" * 30) if choice == "1":
for line in file: roll = input("Enter roll number: ")
roll_no, name, marks = line.strip().split(",") name = input("Enter name: ")
print(f"{roll_no:7} | {name:10} | {marks}") marks = input("Enter marks: ")
except FileNotFoundError: add_student(roll, name, marks)
print("No records found.")
elif choice == "2":
def search_student(roll_no): display_all_students()
try:
with open("students.txt", "r") as file: elif choice == "3":
for line in file: roll = input("Enter roll number to search: ")
data = line.strip().split(",") search_student(roll)
if data[0] == roll_no:
print(f"\nStudent Found:") elif choice == "4":
print(f"Roll No: {data[0]}") print("Exiting program. Goodbye!")
print(f"Name: {data[1]}") break
print(f"Marks: {data[2]}")
return else:
print(f"No student with roll no {roll_no} found.") print("Invalid choice. Please try again.")
except FileNotFoundError:
print("No records found.")
File Handling Best Practices

Optimize for Efficiency

1 Process large files in chunks, close files promptly

Handle Errors Robustly


2 Use try-except blocks for all file operations

Manage Resources Properly


Use with statement (context managers) for auto-closing

Choose Appropriate File Types


Text for human-readable data, binary for media/complex data
Security Considerations
Input Validation Path Traversal Prevention
• Always validate file paths • Avoid using raw user input in
• Never trust user input for paths
filenames • Use os.path.basename() to get
• Check file extensions for filename only
expected types • Restrict file operations to
specific directories

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

Test all operations


Verify reading, writing, appending, and seeking

Test error conditions


Ensure proper handling of missing files, permission issues

Clean up after tests


Remove test files to keep environment clean
Temporary Files
What Are Temporary Files? Python's tempfile Module

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:

• Storing intermediate processing results • tempfile.TemporaryFile(): Creates a file that's automatically


• Caching data temporarily deleted

• • tempfile.NamedTemporaryFile(): Creates a named temporary


Transferring data between program components
file
• tempfile.TemporaryDirectory(): Creates a temporary directory
Example: Using Temporary Files
Code Example Temporary Directory Example

import tempfile import tempfile


import os import os
import shutil
# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False) as temp: # Create a temporary directory
print(f"Created temporary file: {temp.name}") with tempfile.TemporaryDirectory() as temp_dir:
print(f"Created temporary directory: {temp_dir}")
# Write data to the temporary file
temp.write(b"Line 1: Temporary data\n") # Create some files in the temporary directory
temp.write(b"Line 2: More data\n") for i in range(3):
temp.write(b"Line 3: Final data\n") file_path = os.path.join(temp_dir, f"file_{i}.txt")
with open(file_path, 'w') as f:
# Remember the name for later f.write(f"Content of temporary file {i}")
temp_name = temp.name
# List files in the temporary directory
# File is closed but not deleted (delete=False) files = os.listdir(temp_dir)
print(f"Files in temporary directory: {files}")
# Open and read the temporary file
with open(temp_name, 'r') as file: # Directory and contents automatically deleted
content = file.read() print("Temporary directory has been removed")
print("\nContent of temporary file:")
print(content)

# Manually delete the temporary file


os.unlink(temp_name)
print(f"Deleted temporary file: {temp_name}")
File Compression
Benefits of Compression
Reduces storage requirements and speeds up file transfers

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

import zipfile import zipfile


import os
# Open an existing ZIP file
# Files to compress with zipfile.ZipFile("archive.zip", "r") as zip_file:
files_to_zip = ["data.txt", "image.jpg", "report.pdf"] # List contents of the ZIP
print("\nContents of the ZIP file:")
# Create a new ZIP file for file_info in zip_file.infolist():
with zipfile.ZipFile("archive.zip", "w") as zip_file: print(f" - {file_info.filename} "
# Add files to the ZIP f"({file_info.file_size} bytes, "
for file in files_to_zip: f"compressed: {file_info.compress_size} bytes)")
if os.path.exists(file):
zip_file.write(file) # Extract all files
print(f"Added {file} to archive") print("\nExtracting all files...")
else: zip_file.extractall("extracted_files")
print(f"Warning: {file} not found")
# Extract a specific file
# Add a file with a different name in the archive if "report.pdf" in zip_file.namelist():
if os.path.exists("config.json"): zip_file.extract("report.pdf", "specific_extract")
zip_file.write("config.json", print("Extracted report.pdf to specific_extract folder")
arcname="settings/config.json")
print("Added config.json as settings/config.json")
Working with Large Files
Chunk-Based Processing
Read and write large files in manageable chunks to avoid memory
overload

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

def process_large_file(input_file, output_file, def line_processor(filename):


chunk_size=1024*1024): """Generator that yields lines one at a time.
"""Process a large file in chunks (1MB default).""" Good for processing large text files line by line.
with open(input_file, 'rb') as infile: """
with open(output_file, 'wb') as outfile: with open(filename, 'r') as file:
while True: for line in file: # Python reads line by line
# Read chunk # Skip empty lines
chunk = infile.read(chunk_size) if not line.strip():
if not chunk: continue
break
# Example processing: Find lines with "data"
# Process chunk (example: uppercase text) if "data" in line.lower():
# For binary processing, you'd use different # Process and yield the line
# transformations yield line.strip()
if b'text' in chunk.lower():
# Example text processing # Usage - memory efficient even for gigabyte files
processed = chunk.upper() for processed_line in line_processor('huge_log.txt'):
else: print(f"Found data line: {processed_line[:50]}...")
processed = chunk
# Further processing on each line
# Write processed chunk words = processed_line.split()
outfile.write(processed) if len(words) > 10:
print(f" This line has {len(words)} words")
# Usage
process_large_file('large_input.txt', 'large_output.txt')
Memory-Mapped Files

Direct Memory Performance Random Access


Access Benefits
Efficient seeking within
Maps file contents Faster random access large files
directly to memory for for large files
efficient access

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

# Create a sample file for demonstration • Database implementations


with open("mmap_example.txt", "w") as f:
f.write("This is line 1\n") • Real-time data processing
f.write("This is line 2\n")
• Scientific computing with large datasets
f.write("This is line 3\n")
f.write("This is line 4\n") • Application cache mechanisms
f.write("This is line 5\n")
However, they come with some considerations:
# Memory-map the file
with open("mmap_example.txt", "r+b") as f:
• Limited by available virtual memory
# Get file size
size = os.path.getsize("mmap_example.txt") • Potential for segmentation faults if not used carefully
# Create memory map • May not be as beneficial for sequential access as regular file I/O
mm = mmap.mmap(f.fileno(), size)

# Read content - like normal file


print("Entire content:")
print(mm.read().decode())

# Reset position
mm.seek(0)

# Random access - find pattern


pattern = b"line 3"
position = mm.find(pattern)

if position != -1:
mm.seek(position)
# Read 20 bytes from the pattern position
print(f"\nFound at position {position}:")
print(mm.read(20).decode())

# Modify content (if opened with r+b)


mm.seek(0)
modified = mm.read().decode().replace("line", "row")
mm.seek(0)
mm.write(modified.encode())

# Close the map


mm.close()
Remote File Operations

Cloud Storage Integration


AWS S3, Google Cloud Storage, Azure Blob Storage

Remote Protocols
FTP, SFTP, HTTP/HTTPS for remote files

Network File Systems


Mounting remote filesystems locally

Local File Operations


4 Basic file I/O within local system
Example: Remote File Access
HTTP File Download FTP File Upload

import requests from ftplib import FTP

def download_file(url, local_filename): def upload_file_ftp(hostname, username, password,


"""Download a file from URL to local path.""" local_file, remote_path):
# Send HTTP request """Upload a file to FTP server."""
response = requests.get(url, stream=True) try:
# Connect to FTP server
# Check if request was successful ftp = FTP(hostname)
response.raise_for_status() ftp.login(username, password)

# Open local file for writing # Change to target directory


with open(local_filename, 'wb') as f: if remote_path:
# Download in chunks and write to file ftp.cwd(remote_path)
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk) # Open local file for reading in binary mode
with open(local_file, 'rb') as file:
return local_filename # Upload the file
# STOR command is used to upload a file
# Usage example ftp.storbinary(f'STOR {local_file}', file)
try:
file_url = "https://example.com/data.csv" # Close connection
local_file = "downloaded_data.csv" ftp.quit()
download_file(file_url, local_file) return True
print(f"Successfully downloaded to {local_file}")
except Exception as e:
except requests.exceptions.RequestException as e: print(f"FTP upload failed: {e}")
print(f"Download failed: {e}") return False

# 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

# pip install watchdog File monitoring is useful in many scenarios:


from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler • Automatic build or test triggers when source files change
import time • Log file monitoring for real-time alerting
class MyHandler(FileSystemEventHandler): • Configuration file watching to reload settings
"""Handler for file system events"""
• Directory synchronization for backup systems
def on_created(self, event):
• Hot-reloading during development
"""Called when a file or directory is created"""
if not event.is_directory:
print(f"Created: {event.src_path}") The watchdog library provides cross-platform file system monitoring, making it easy to react to changes in the file system. This example monitors the
# You could process the new file here current directory and its subdirectories, printing messages when files are created, modified, or deleted.

def on_modified(self, event):


"""Called when a file or directory is modified"""
if not event.is_directory:
print(f"Modified: {event.src_path}")
# You could reload the file here

def on_deleted(self, event):


"""Called when a file or directory is deleted"""
if not event.is_directory:
print(f"Deleted: {event.src_path}")
# You could handle removal here

if __name__ == "__main__":
# Path to monitor
path = "." # Current directory

# Initialize event handler


event_handler = MyHandler()

# Initialize Observer
observer = Observer()
observer.schedule(event_handler, path, recursive=True)

# Start the observer


observer.start()
print(f"Monitoring directory: {path}")

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

import os def set_file_permissions(filepath, mode):


import stat """Set file permissions using octal mode."""
try:
def check_file_permissions(filepath): os.chmod(filepath, mode)
"""Check and display file permissions.""" return f"Changed permissions of {filepath} to {oct(mode)}"
if not os.path.exists(filepath): except Exception as e:
return f"File {filepath} does not exist" return f"Error changing permissions: {e}"

# Get file stats # Example usage


file_stats = os.stat(filepath) filename = "example.txt"
mode = file_stats.st_mode
# Create a test file
# Check type with open(filename, "w") as f:
file_type = "Directory" if stat.S_ISDIR(mode) else "File" f.write("Test content for permission example")

# Format permissions string (Unix-style) # Check initial permissions


perms = "" print(check_file_permissions(filename))
perms += "r" if mode & stat.S_IRUSR else "-"
perms += "w" if mode & stat.S_IWUSR else "-" # Make file read-only
perms += "x" if mode & stat.S_IXUSR else "-" print(set_file_permissions(filename, 0o444))
perms += "r" if mode & stat.S_IRGRP else "-"
perms += "w" if mode & stat.S_IWGRP else "-" # Check updated permissions
perms += "x" if mode & stat.S_IXGRP else "-" print(check_file_permissions(filename))
perms += "r" if mode & stat.S_IROTH else "-"
perms += "w" if mode & stat.S_IWOTH else "-" # Make file read-write
perms += "x" if mode & stat.S_IXOTH else "-" print(set_file_permissions(filename, 0o644))

# Check access for current user # Check final permissions


readable = os.access(filepath, os.R_OK) print(check_file_permissions(filename))
writable = os.access(filepath, os.W_OK)
executable = os.access(filepath, os.X_OK)

result = (f"{file_type}: {filepath}\n"


f"Permissions: {perms} ({oct(mode & 0o777)})\n"
f"Current user access:\n"
f" Read: {'Yes' if readable else 'No'}\n"
f" Write: {'Yes' if writable else 'No'}\n"
f" Execute: {'Yes' if executable else 'No'}")

return result
Handling Special File Types

Image Files Excel Files Database Files


Using PIL/Pillow library Using pandas, Using sqlite3 for local
for image processing openpyxl, or xlrd/xlwt database files

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)

# Save updated file


read_df.to_excel("students_updated.xlsx",
index=False)
print("\nUpdated Excel file saved")

# Run the example


excel_file_operations()
File Handling in Web Applications

File Uploads File Downloads


Handling user uploads via forms Serving files to users

Security Considerations 4 Storage Management


Validation and access control Organizing and securing files
Summary: File Handling in Python
File Basics 1
Understanding file types, open/close operations, and modes

2 Reading and Writing


Text and binary file I/O, positioning, and seeking

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.

You might also like