10 Python Automation Scripts
10 Python Automation Scripts
Single Recipient
import smtplib
from email.mime.text import MIMEText
# Sending email
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, to_email, msg.as_string())
# Example usage
send_email('Automation Test', 'This is an automated message.', 'recipient@example.com')
Multiple Recipients
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
# Example usage
recipients_list = ['recipient1@example.com', 'recipient2@example.com']
send_individual_emails('Enroll Now: Batch-7 Starting on 2nd November', 'Batch-7 is starting soon!',
recipients_list, 'Batch-7-Syllabus.pdf')
Script 2 | Web Scraping
pip install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
def scrape_headlines_demo(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
headlines = soup.find_all('h2')
for idx, headline in enumerate(headlines, 1):
print(f'{idx}: {headline.text.strip()}')
# Example usage
scrape_headlines_demo('https://www.indiatoday.in/india')
# Example usage
move_files_by_type('/path/to/source', '/path/to/destination', '.txt')
Script 4 | Scaling AKS Resources Based on Weather
import requests
import subprocess
def get_weather_data(city):
api_key = 'your_weatherapi_key'
base_url = f'http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}'
response = requests.get(base_url)
data = response.json()
# Example usage
get_weather_data('London')
pairs = [
[r"(.*)help(.*)", ["How can I assist you today?"]],
[r"(.*)price of (.*)", ["The price of %2 is $50."]],
[r"(.*)course(.*)", ["Check out the DevOps course here: https://devopsshack.com"]],
[r"quit", ["Goodbye!"]]
]
def basic_chatbot():
print("Welcome to Customer Support! (type 'quit' to exit)")
chat = Chat(pairs, reflections)
chat.converse()
if slack_response.status_code == 200:
print(f"Slack notification sent: {message}")
else:
print(f"Failed to send Slack notification. Status Code: {slack_response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
# Example usage
server_url = 'https://your-server-url.com'
slack_webhook = 'https://hooks.slack.com/services/XXXX/XXXX/XXXX'
monitor_server_health(server_url, slack_webhook)
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
backup_zip_filename = os.path.join(backup_folder, f"backup_{timestamp}.zip")
# Example usage
backup_and_zip_files('/path/to/source', '/path/to/backup')
Script 8 | Cleanup Directory on Condition
import os
import time
# Example usage
cleanup_old_files('/path/to/directory', 30)
# Example usage
download_latest_videos("https://www.youtube.com/feeds/videos.xml?channel_id=YOUR_CHANNEL
_ID", 'downloads')
Script 10 | Synchronizing Local Repo with Remote
Repo
import os
# Example usage
repositories = [
{"url": "https://github.com/your_username/repo1.git", "dir": "/path/to/repo1"},
{"url": "https://github.com/your_username/repo2.git", "dir": "/path/to/repo2"}
]