Practical Examples
Real-World File Type Selection Scenarios
graph TD
A[Practical File Selection] --> B[Log File Processing]
A --> C[Image Management]
A --> D[Backup and Archiving]
A --> E[Data Analysis]
1. Log File Processing
import os
import re
from datetime import datetime
def process_error_logs(directory):
error_logs = []
for filename in os.listdir(directory):
if filename.endswith('.log'):
full_path = os.path.join(directory, filename)
with open(full_path, 'r') as file:
for line in file:
if 'ERROR' in line:
error_logs.append({
'filename': filename,
'error_message': line.strip()
})
return error_logs
## Example usage
logs = process_error_logs('/var/log/myapp')
print(logs)
2. Image File Management
import os
from PIL import Image
def organize_images(source_dir, target_dir):
image_extensions = ['.jpg', '.png', '.gif', '.jpeg']
for filename in os.listdir(source_dir):
file_ext = os.path.splitext(filename)[1].lower()
if file_ext in image_extensions:
source_path = os.path.join(source_dir, filename)
with Image.open(source_path) as img:
## Organize by image dimensions
size_category = 'large' if img.width > 1920 else 'small'
target_path = os.path.join(target_dir, size_category, filename)
os.makedirs(os.path.dirname(target_path), exist_ok=True)
## Copy or move file
os.rename(source_path, target_path)
3. Data Analysis File Selection
import pandas as pd
import os
def select_csv_files(directory):
csv_files = []
for filename in os.listdir(directory):
if filename.endswith('.csv'):
file_path = os.path.join(directory, filename)
try:
df = pd.read_csv(file_path)
csv_files.append({
'filename': filename,
'rows': len(df),
'columns': len(df.columns)
})
except Exception as e:
print(f"Error processing {filename}: {e}")
return csv_files
## Example usage
data_files = select_csv_files('/home/user/datasets')
File Selection Best Practices
Practice |
Description |
Recommendation |
Error Handling |
Manage file access exceptions |
Use try-except blocks |
Performance |
Optimize file scanning |
Use generators for large directories |
Flexibility |
Support multiple file types |
Create configurable selection methods |
Advanced Techniques
- Use
pathlib
for cross-platform path handling
- Implement caching for repeated file selections
- Add logging for file processing operations
LabEx recommends practicing these techniques to become proficient in file type selection.