Super Useful Python Automation Scripts That We Can Use Daily (With Source Code)

Automation has become an integral part of our daily lives. From automated emails to automatic reminders, automation helps us save time and increase productivity. In this blog, we will discuss some super useful python automation scripts that we can use daily, along with their source code.

Automated Email Responses

Automated email responses can be helpful when you’re out of the office or unable to respond to emails immediately. This Python script automatically responds to emails with a customizable message.

import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("youremail@gmail.com", "yourpassword")

msg = "Hello! I'm currently out of the office and will respond to your email as soon as possible."

server.sendmail("youremail@gmail.com", "recipientemail@gmail.com", msg)
server.quit()

Daily Reminder

A daily reminder script can help you stay on top of your daily tasks. This Python script sends a customizable reminder email every day at a specified time.

import schedule
import time
import smtplib

def send_email():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("youremail@gmail.com", "yourpassword")

    msg = "Don't forget to complete your daily tasks!"

    server.sendmail("youremail@gmail.com", "recipientemail@gmail.com", msg)
    server.quit()

schedule.every().day.at("09:00").do(send_email)

while True:
    schedule.run_pending()
    time.sleep(1)

Social Media Automation

Social media automation can save you time and increase your social media presence. This Python script automatically posts to Twitter with a customizable message and image.

import tweepy
import os

auth = tweepy.OAuthHandler(os.environ['CONSUMER_KEY'], os.environ['CONSUMER_SECRET'])
auth.set_access_token(os.environ['ACCESS_TOKEN'], os.environ['ACCESS_TOKEN_SECRET'])

api = tweepy.API(auth)

tweet = "Check out my latest blog post! #blogging #automation"
image_path = "image.jpg"

api.update_with_media(image_path, status=tweet)

File Backup

Automatically backing up important files can save you from losing important data. This Python script automatically backs up files to Google Drive.

import os
import shutil
import zipfile
import datetime
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

now = datetime.datetime.now()
date_time = now.strftime("%m_%d_%Y_%H_%M_%S")

folder_name = "backup_" + date_time
folder = drive.CreateFile({'title': folder_name, "mimeType": "application/vnd.google-apps.folder"})
folder.Upload()

file_names = ["file1.txt", "file2.txt", "file3.txt"]
for file_name in file_names:
    shutil.copy2(file_name, folder_name)

zip_name = folder_name + ".zip"
with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zip_file:
    for root, dirs, files in os.walk(folder_name):
        for file in files:
            zip_file.write(os.path.join(root, file))

backup_file = drive.CreateFile({'title': zip_name})
backup_file.Upload()

shutil.rmtree(folder_name)
os.remove(zip_name)

Website Monitoring

Monitoring your website’s uptime and performance can help you identify issues before they become major problems. This Python script monitors your website and sends a notification if it’s down.

import requests
import smtplib

def send_email():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("youremail@gmail.com", "yourpassword")

    msg = "Your website is down!"

    server.sendmail("youremail@gmail.com", "recipientemail@gmail.com", msg)
    server.quit()

website_url = "https://www.example.com"
timeout = 5

while True:
    try:
        response = requests.get(website_url, timeout=timeout)
    except requests.ConnectionError:
        send_email()
    else:
        status_code = response.status_code
        if status_code != 200:
            send_email()
    time.sleep(60)

Conclusion Automation scripts can help us save time, increase productivity, and reduce the risk of human error. By using the above scripts or creating your own, you can automate daily tasks, improve your workflow, and ultimately increase your efficiency.