Automation
Automation Basics
Use Python to automate common tasks like renaming files, sending emails, or running scheduled jobs using built-in libraries such as os and smtplib.
Automation
Python is an excellent tool for automating repetitive or manual tasks, such as renaming files, processing text, sending emails, or interacting with APIs. This guide introduces the basics of writing automation scripts with Python.
Example: Renaming Multiple Files
You can use Python’s os
module to batch rename files in a folder.
pythonCopyEditimport os folder = "images" for index, filename in enumerate(os.listdir(folder)): if filename.endswith(".jpg"): new_name = f"photo_{index + 1}.jpg" os.rename( os.path.join(folder, filename), os.path.join(folder, new_name) )
Example: Sending Emails Automatically
pythonCopyEditimport smtplib from email.message import EmailMessage msg = EmailMessage() msg.set_content("This is an automated message.") msg["Subject"] = "Automation Test" msg["From"] = "you@example.com" msg["To"] = "friend@example.com" with smtplib.SMTP("smtp.example.com", 587) as server: server.starttls() server.login("you@example.com", "yourpassword") server.send_message(msg)
⚠️ Use environment variables or secure vaults for storing passwords.
Best Practices
Use
argparse
to pass input from the command line.Combine with cron jobs or Task Scheduler for timed automation.
Add logging to track what the script does over time.
Conclusion
Whether you’re managing files, generating reports, or sending reminders, Python scripts can save hours of manual effort. In the next article, we’ll explore how to build flexible scripts using command-line arguments.
Automation
Python is an excellent tool for automating repetitive or manual tasks, such as renaming files, processing text, sending emails, or interacting with APIs. This guide introduces the basics of writing automation scripts with Python.
Example: Renaming Multiple Files
You can use Python’s os
module to batch rename files in a folder.
pythonCopyEditimport os folder = "images" for index, filename in enumerate(os.listdir(folder)): if filename.endswith(".jpg"): new_name = f"photo_{index + 1}.jpg" os.rename( os.path.join(folder, filename), os.path.join(folder, new_name) )
Example: Sending Emails Automatically
pythonCopyEditimport smtplib from email.message import EmailMessage msg = EmailMessage() msg.set_content("This is an automated message.") msg["Subject"] = "Automation Test" msg["From"] = "you@example.com" msg["To"] = "friend@example.com" with smtplib.SMTP("smtp.example.com", 587) as server: server.starttls() server.login("you@example.com", "yourpassword") server.send_message(msg)
⚠️ Use environment variables or secure vaults for storing passwords.
Best Practices
Use
argparse
to pass input from the command line.Combine with cron jobs or Task Scheduler for timed automation.
Add logging to track what the script does over time.
Conclusion
Whether you’re managing files, generating reports, or sending reminders, Python scripts can save hours of manual effort. In the next article, we’ll explore how to build flexible scripts using command-line arguments.
Automation
Python is an excellent tool for automating repetitive or manual tasks, such as renaming files, processing text, sending emails, or interacting with APIs. This guide introduces the basics of writing automation scripts with Python.
Example: Renaming Multiple Files
You can use Python’s os
module to batch rename files in a folder.
pythonCopyEditimport os folder = "images" for index, filename in enumerate(os.listdir(folder)): if filename.endswith(".jpg"): new_name = f"photo_{index + 1}.jpg" os.rename( os.path.join(folder, filename), os.path.join(folder, new_name) )
Example: Sending Emails Automatically
pythonCopyEditimport smtplib from email.message import EmailMessage msg = EmailMessage() msg.set_content("This is an automated message.") msg["Subject"] = "Automation Test" msg["From"] = "you@example.com" msg["To"] = "friend@example.com" with smtplib.SMTP("smtp.example.com", 587) as server: server.starttls() server.login("you@example.com", "yourpassword") server.send_message(msg)
⚠️ Use environment variables or secure vaults for storing passwords.
Best Practices
Use
argparse
to pass input from the command line.Combine with cron jobs or Task Scheduler for timed automation.
Add logging to track what the script does over time.
Conclusion
Whether you’re managing files, generating reports, or sending reminders, Python scripts can save hours of manual effort. In the next article, we’ll explore how to build flexible scripts using command-line arguments.