0% found this document useful (0 votes)
10 views3 pages

CBS U3-Ap

The document provides advanced techniques for file handling and exception management in Python, including asynchronous file operations with aiofiles, using pathlib for modern file handling, and multithreading for processing multiple files. It also covers secure file handling with cryptography, logging with rotating log files, cloud file operations using boto3, and automating file cleanup with the schedule module. Additionally, it discusses AI-driven exception detection, real-time error monitoring with Sentry, and implementing transactional file handling for rollback on failure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

CBS U3-Ap

The document provides advanced techniques for file handling and exception management in Python, including asynchronous file operations with aiofiles, using pathlib for modern file handling, and multithreading for processing multiple files. It also covers secure file handling with cryptography, logging with rotating log files, cloud file operations using boto3, and automating file cleanup with the schedule module. Additionally, it discusses AI-driven exception detection, real-time error monitoring with Sentry, and implementing transactional file handling for rollback on failure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Advanced Python: Beyond-the-Syllabus - File Handling & Exceptions

1. File Streaming with Async IO

Use aiofiles for non-blocking file operations.


Example:
import aiofiles, asyncio

async def read_file():


async with aiofiles.open('large_file.txt', mode='r') as f:
async for line in f:
print(line.strip())

asyncio.run(read_file())

2. Using pathlib Over os.path

Modern OOP-style file handling:


from pathlib import Path
path = Path("example.txt")
if path.exists():
print(path.read_text())

3. Multithreaded File Operations

For parallel processing of multiple files:


from concurrent.futures import ThreadPoolExecutor
import os

def read_and_process(filename):
with open(filename, 'r') as f:
return f.read().count("ERROR")

files = os.listdir('logs')
with ThreadPoolExecutor() as executor:
results = executor.map(read_and_process, files)

4. Encrypted File Handling

Use cryptography to read/write secure data:


from cryptography.fernet import Fernet

key = Fernet.generate_key()
f = Fernet(key)

with open("secret.txt", "wb") as file:


file.write(f.encrypt(b"Top Secret"))

5. Rotating Log Files


Advanced Python: Beyond-the-Syllabus - File Handling & Exceptions

Use RotatingFileHandler for production logging:


import logging
from logging.handlers import RotatingFileHandler

handler = RotatingFileHandler("app.log", maxBytes=10000, backupCount=5)


logging.basicConfig(handlers=[handler], level=logging.INFO)
logging.info("Log entry.")

6. Cloud File Handling

Use boto3 for reading from AWS S3:


import boto3
s3 = boto3.client('s3')
s3.download_file('mybucket', 'data.json', 'local_copy.json')

7. AI-Driven Exception Detection

Train ML models to detect patterns in error logs.


Example: Use scikit-learn RandomForest to predict error types.

8. Scheduled File Cleanup

Automate with `schedule` module:


import schedule, os, time

def clean_logs():
for file in os.listdir('logs'):
if file.endswith('.log'):
os.remove(os.path.join('logs', file))

schedule.every().day.at("23:00").do(clean_logs)
while True:
schedule.run_pending()
time.sleep(60)

9. Exception Telemetry & Monitoring

Integrate with Sentry for real-time error reporting:


import sentry_sdk
sentry_sdk.init("https://<your_sentry_dsn>")
try:
1/0
except ZeroDivisionError:
sentry_sdk.capture_exception()

10. Transactional File Handling

Rollback on failure:
Advanced Python: Beyond-the-Syllabus - File Handling & Exceptions

import shutil

try:
shutil.copy('important.txt', 'backup.txt')
raise ValueError("Something went wrong")
except Exception:
shutil.copy('backup.txt', 'important.txt')

You might also like