Python Automation
Automation is one of the most powerful uses of Python.
Instead of doing repetitive tasks manually, Python allows you to:
- Automate file management
- Send emails automatically
- Scrape websites
- Process Excel files
- Control system tasks
- Schedule jobs
Python is widely used in automation because it is:
- Simple
- Powerful
- Cross-platform
- Rich in libraries
What is Automation?
Automation means:
Using scripts or programs to perform tasks without human intervention.
Why Use Python for Automation?
Python is ideal for automation because:
- Easy syntax
- Huge library ecosystem
- Strong community support
- Works on Windows, Linux, Mac
- Integrates with APIs and systems
1. File Automation
Renaming Files Automatically
import os
folder = "files"
for i, file in enumerate(os.listdir(folder)):
new_name = f"file_{i}.txt"
os.rename(
os.path.join(folder, file),
os.path.join(folder, new_name)
)2. File Creation Automation
for i in range(5):
with open(f"file_{i}.txt", "w") as f:
f.write("Hello Automation")3. Web Automation
Using requests:
import requests
response = requests.get("https://example.com")
print(response.status_code)4. Web Scraping Automation
from bs4 import BeautifulSoup
import requests
url = "https://example.com"
page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
print(soup.title.text)5. Email Automation
import smtplib
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("your_email@gmail.com", "password")
server.sendmail(
"from@gmail.com",
"to@gmail.com",
"Hello from Python Automation"
)
server.quit()6. Excel Automation
Using openpyxl:
from openpyxl import Workbook
wb = Workbook()
sheet = wb.active
sheet["A1"] = "Name"
sheet["B1"] = "Score"
wb.save("data.xlsx")7. System Automation
Get system info
import os
print(os.name)
print(os.getcwd())8. Scheduling Automation Tasks
Using schedule:
import schedule
import time
def job():
print("Running scheduled task")
schedule.every(5).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)9. Browser Automation (Selenium)
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://google.com")10. API Automation
import requests
data = {
"name": "John"
}
response = requests.post(
"https://example.com/api",
json=data
)
print(response.json())11. Real-World Automation Examples
Python automation is used in:
- Data entry automation
- Social media posting
- Report generation
- Email marketing
- Web scraping bots
- DevOps scripts
12. Advantages of Automation
- Saves time
- Reduces human error
- Increases productivity
- Works 24/7
- Scalable workflows
13. Common Mistakes
Hardcoding paths
folder = "/Users/name/Desktop"Use dynamic paths instead.
Not handling errors
Always use try-except in automation scripts.
try:
print("Running task")
except Exception as e:
print(e)Best Practices
- Use virtual environments
- Log automation results
- Handle exceptions properly
- Use scheduling wisely
- Keep scripts modular
Summary
Python automation allows developers to eliminate repetitive tasks and increase productivity. With powerful libraries like os, requests, selenium, and openpyxl, you can automate almost anything.
Conclusion
Python is one of the best tools for automation. Whether you're working with files, web data, emails, or system tasks, Python helps you build efficient and scalable automation solutions with minimal effort.


0 Comments