Python Automation
1. Python File Automation (os, shutil)
Create a new file
f = open("sample.txt", "w")
f.write("Hello")
f.close()
Read file
with open("sample.txt", "r") as f:
print(f.read())
Append data
with open("sample.txt", "a") as f:
f.write("\nNew line added")
2. OS Module (Very Important)
import os
Create folder
os.mkdir("backup")
Create multiple folders
os.makedirs("project/logs")
List files
print(os.listdir())
Rename file
os.rename("sample.txt", "data.txt")
Delete file
os.remove("data.txt")
Remove folder
os.rmdir("backup")
3. shutil Module
Advanced file handling.
import shutil
Copy file
shutil.copy("file1.txt", "backup/file1.txt")
Copy folder
shutil.copytree("project", "project_backup")
Delete folder
shutil.rmtree("project_backup")
4. Automating Downloads
With requests module:
import requests
url = "https://example.com/file.pdf"
r = requests.get(url)
with open("file.pdf", "wb") as f:
f.write(r.content)
5. Web Scraping Automation (BeautifulSoup)
Install:
pip install requests beautifulsoup4
Scraping Example
import requests
from bs4 import BeautifulSoup
url = "https://www.example.com"
html = requests.get(url).text
soup = BeautifulSoup(html, "html.parser")
title = soup.find("h1").text
print(title)
6. API Automation
Using weather API example:
import requests
url = "https://api.openweathermap.org/data/2.5/weather?q=Chennai&appid=API_KEY"
data = requests.get(url).json()
print(data["weather"][0]["main"])
7. Send Email Using Python
SMTP Email Automation
import smtplib
from email.mime.text import MIMEText
sender = "you@gmail.com"
password = "yourpassword"
receiver = "test@gmail.com"
msg = MIMEText("Hello, this is automated mail")
msg["Subject"] = "Test Email"
msg["From"] = sender
msg["To"] = receiver
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender, password)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
8. Scheduling Automation Scripts
Windows Task Scheduler
- Open Task Scheduler
- Create Basic Task
- Choose frequency (daily/hourly)
- Choose “Start a program” → select python.exe & your script Linux Cron Job Open cron: crontab -e Run script every 5 minutes: */5 * * * * python3 /home/user/script.py
9. DevOps Real-Time Automation Examples
A. Server Health Monitoring Script
import psutil
print("CPU:", psutil.cpu_percent())
print("RAM:", psutil.virtual_memory().percent)
B. Log File Monitoring
with open("/var/log/syslog") as log:
for line in log:
if "error" in line.lower():
print("Error found:", line)
C. Automate File Backup
import shutil
source = "data/"
dest = "backup/"
shutil.copytree(source, dest, dirs_exist_ok=True)
print("Backup complete")
D. Check Website Status
import requests
url = "https://google.com"
res = requests.get(url)
if res.status_code == 200:
print("Website Up")
else:
print("Website Down")
E. Auto-Restart Service (Linux)
import os
status = os.system("systemctl is-active --quiet nginx")
if status != 0:
os.system("systemctl restart nginx")
10. JSON Automation (Data Processing)
import json
f = open("data.json")
data = json.load(f)
print(data["name"])
11. CSV Automation
import csv
with open("data.csv") as f:
reader = csv.reader(f)
for row in reader:
print(row)
Top comments (0)