OS Automation & Logging
One of the most practical everyday automations in Python is an Intelligent File Organizer that monitors your Downloads or Desktop directory and automatically sorts files into dedicated subfolders based on extension type (Documents, Images, Archives, Code, Videos).
# Intelligent Directory Organizer Pipeline:
import os
from pathlib import Path
FILE_CATEGORIES = {
"Documents": [".pdf", ".docx", ".txt", ".xlsx", ".pptx"],
"Images": [".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg"],
"Archives": [".zip", ".tar", ".gz", ".rar", ".7z"],
"Code": [".py", ".js", ".html", ".css", ".json", ".sql"],
"Videos": [".mp4", ".mkv", ".mov", ".avi"]
}
def classify_file(filename):
ext = Path(filename).suffix.lower()
for category, extensions in FILE_CATEGORIES.items():
if ext in extensions:
return category
return "Other"
# Simulate sorting cluttered files:
mock_download_folder = [
"Machine_Learning_Notes.pdf",
"avatar_profile.png",
"backup_2026_08_14.zip",
"main_script.py",
"tutorial_video.mp4",
"financial_budget.xlsx",
"random_unknown_file.xyz"
]
print("--- ๐๏ธ Automated File Sorting & Classification Output ---")
for file in mock_download_folder:
dest_folder = classify_file(file)
print(f"โข Moving: '{file:28}' โโโบ ๐ [{dest_folder}/]")
In real OS scripts, combine Path(dest_dir).mkdir(exist_ok=True) with shutil.move(src_path, dest_path) to create destination folders and move files on disk.
In professional production systems, never use print() for application monitoring! print() lacks timestamps, severity levels, log file rotation, and cannot be filtered dynamically.
The 5 Standard Python Logging Levels:
| Level | Numeric Value | When to Use |
|---|---|---|
DEBUG | 10 | Detailed diagnostic information for developers during debugging. |
INFO | 20 | Confirmation that normal operational milestones are proceeding as expected. |
WARNING | 30 | Indication of something unexpected or a runtime warning (e.g. low disk space). |
ERROR | 40 | A serious problem that prevented a specific operation from executing. |
CRITICAL | 50 | A fatal error causing the entire program or service to terminate immediately. |
import logging
import sys
# 1. Configure production-standard Logger with Formatter:
logger = logging.getLogger("OurCompilerApp")
logger.setLevel(logging.DEBUG)
# Create console handler with structured format:
console_handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("[%(asctime)s] [%(levelname)-8s] [%(name)s]: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
# 2. Log events across all severity levels:
logger.debug("Connecting to Redis cache on 127.0.0.1:6379...")
logger.info("Database connection established successfully.")
logger.warning("Memory usage exceeded 75% threshold.")
logger.error("Failed to process payment for user #9412: Bank gateway timeout.")
logger.critical("Primary database server unreachable! Failing over to replica...")
Use logging.handlers.RotatingFileHandler("app.log", maxBytes=10*1024*1024, backupCount=5) to ensure log files never grow infinitely and consume all disk space.
Writing verbose DEBUG logs on every single web request generates gigabytes of disk I/O, slowing down server response times. Always set logger.setLevel(logging.INFO) or logging.WARNING in production.
Build a configuration loader function load_app_config(env_mode) that returns production or development settings based on an input string.
def load_app_config(env_mode="development"):
if env_mode == "production":
return {"debug": False, "db": "postgresql://prod_db:5432", "log_level": "WARNING"}
return {"debug": True, "db": "sqlite:///:memory:", "log_level": "DEBUG"}
print("Development Config:", load_app_config("development"))
print("Production Config: ", load_app_config("production"))
Q What is the schedule library in Python?
The schedule library is a clean, human-readable job scheduling package: schedule.every().day.at("10:30").do(job) or schedule.every(10).minutes.do(backup).
Q Why should I use TOML or YAML over JSON for configuration files?
TOML and YAML support inline comments (# comment), multi-line strings, and clean human-friendly indentation, making them superior for developer configuration files.
Q What does python-dotenv do?
python-dotenv reads key-value pairs from a local .env file and automatically injects them into os.environ at application startup.