๐ Lesson 29: Python Automation – Automate Files, Folders, Emails & More
Welcome to Lesson 29! Today we’ll explore how Python can automate daily tasks — file management, sending emails, working with folders, and more. Automation helps you save time, remove repetitive work, and build powerful scripts for real-world usage.
๐ก Quick Benefits of Python Automation
- Save hours of repetitive work
- Automate daily office tasks (files, emails, screenshots)
- Automate reports, data collection, and emails
- Build powerful bots and assistants for personal or work use
- Control your computer programmatically for streamlined workflows
๐ 1. Automating Files & Folders
๐ฆ Create Folders Automatically
import os
os.makedirs("my_folder", exist_ok=True)
print("Folder created!")
๐ฆ List All Files in a Directory
import os
files = os.listdir(".")
print(files)
๐ฆ Moving & Renaming Files
import os
os.rename("old.txt", "new.txt")
print("File renamed!")
๐ง 2. Sending Emails with Python
Use Python’s smtplib module to send emails.
import smtplib
sender = "youremail@example.com"
password = "YOUR_PASSWORD"
receiver = "friend@example.com"
message = """Subject: Hello from Python
This email was sent using Python automation!
"""
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, receiver, message)
print("Email sent!")
Note: Gmail requires special app passwords or security settings.
๐ 3. Reading Text from PDFs
pip install PyPDF2
import PyPDF2
with open("example.pdf", "rb") as file:
reader = PyPDF2.PdfReader(file)
text = reader.pages[0].extract_text()
print(text)
๐ผ 4. Automating Screenshots
pip install pyautogui
import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")
๐ 5. Automating Website Actions
Using Selenium, you can automate clicking, typing, scraping, and browsing.
pip install selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://google.com")
print(driver.title)
๐ง Why Python Automation Is Powerful
- Save hours of repetitive work
- Automate office tasks
- Automate email reports
- Build bots and assistants
- Control your computer programmatically
๐งช Practice
- Create a script that renames all files in a folder.
- Write a program that takes a screenshot every 10 seconds.
- Automate sending an email (use a test email for safety).
- Extract text from a PDF and save it into a text file.
❓ Common Mistakes
- Forgetting to handle errors in file operations (e.g., missing files or permissions)
- Not setting proper security settings when sending emails (e.g., app passwords)
- Overcomplicating the script—keep automation tasks simple and efficient
❓ Frequently Asked Questions (FAQ)
1. How do I use automation for file management?
Automation can be used to organize files, rename, move, or delete them based on specific conditions or tasks.
2. Can I automate sending emails with attachments?
Yes! You can use Python’s smtplib along with email.mime to send emails with attachments. It is great for automating reports or newsletters.
3. How can I automate web actions?
You can use the selenium library to control a web browser, fill out forms, scrape data, and more.
๐ What’s Next?
In the next lesson, you’ll learn how to:
- Create a project that utilizes automation to interact with websites
- Learn how to build bots that automate repetitive tasks
Comments
Post a Comment