File Automation with pathlib
Python
from pathlib import Path
import shutil
downloads = Path.home() / "Downloads"
categories = {
".pdf": "Documents",
".jpg": "Images", ".jpeg": "Images", ".png": "Images",
".mp4": "Videos",
".zip": "Archives",
}
for file in downloads.iterdir():
if file.is_file() and file.suffix.lower() in categories:
dest_dir = downloads / categories[file.suffix.lower()]
dest_dir.mkdir(exist_ok=True)
shutil.move(str(file), dest_dir / file.name)
print(f"Moved {file.name}")Scheduling Tasks
Python
import schedule
import time
def daily_report():
print("Generating report...")
schedule.every().day.at("09:00").do(daily_report)
schedule.every().hour.do(lambda: print("Heartbeat"))
while True:
schedule.run_pending()
time.sleep(60)Sending Emails
Python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(to, subject, body, smtp_pass):
msg = MIMEMultipart()
msg["From"] = "you@gmail.com"
msg["To"] = to
msg["Subject"] = subject
msg.attach(MIMEText(body, "html"))
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login("you@gmail.com", smtp_pass)
server.sendmail("you@gmail.com", to, msg.as_string())Excel with openpyxl
Python
import openpyxl
from openpyxl.styles import Font, PatternFill
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Report"
headers = ["Product", "Units", "Revenue"]
for col, h in enumerate(headers, 1):
cell = ws.cell(row=1, column=col, value=h)
cell.font = Font(bold=True)
data = [("Widget A", 150, 7500), ("Widget B", 230, 11500)]
for row_idx, row_data in enumerate(data, 2):
for col_idx, value in enumerate(row_data, 1):
ws.cell(row=row_idx, column=col_idx, value=value)
wb.save("report.xlsx")Tip: Store credentials in a .env file and load with python-dotenv β never hardcode passwords in scripts.