Most Python automation tutorials show you code that works in a blog post but breaks in production.
Here's what actually works in real business environments, based on running these scripts daily:
Rule 1: Always Use Proper Logging
Scripts that run silently make debugging a nightmare. Use structured logging from the start:
import logging from datetime import datetime
def setup_logger ( name , log_file = None ): logger = logging . getLogger ( name ) logger . setLevel ( logging . INFO )
# Console handler console = logging . StreamHandler () console . setFormatter ( logging . Formatter ( ' %(asctime)s [%(levelname)s] %(message)s ' , datefmt = ' %Y-%m-%d %H:%M:%S ' )) logger . addHandler ( console )
# File handler (optional but recommended) if log_file : file_handler = logging . FileHandler ( log_file ) file_handler . setFormatter ( logging . Formatter ( ' %(asctime)s [%(levelname)s] %(name)s: %(message)s ' )) logger .…