DEV Community

qing
qing

Posted on

Python Automation for Beginners: 7 Scripts That Actually Work

Python Automation for Beginners: 7 Scripts That Actually Work

As a beginner in the world of Python programming, it can be daunting to know where to start with automation. Python is an excellent language for automating tasks, and with its vast array of libraries and tools, the possibilities are endless. In this article, we will explore 7 practical scripts that can help you get started with Python automation.

Introduction to Automation

Before we dive into the scripts, let's talk about what automation means in the context of Python programming. Automation refers to the process of using Python scripts to perform repetitive tasks, interact with other systems, and simplify workflows. With Python, you can automate tasks such as data entry, file management, email sending, and much more.

Script 1: File Organizer

One of the most useful scripts for beginners is a file organizer. This script can help you organize your files into different folders based on their extensions. Here is an example of how you can create a file organizer script:

import os
import shutil

# Define the folders
folders = {
    'Documents': ['.txt', '.docx', '.pdf'],
    'Images': ['.jpg', '.png', '.gif'],
    'Videos': ['.mp4', '.avi', '.mkv']
}

# Get the current directory
current_dir = os.getcwd()

# Loop through all files in the current directory
for file in os.listdir(current_dir):
    # Get the file extension
    file_ext = os.path.splitext(file)[1]

    # Loop through the folders
    for folder, extensions in folders.items():
        # Check if the file extension matches
        if file_ext in extensions:
            # Move the file to the corresponding folder
            shutil.move(os.path.join(current_dir, file), os.path.join(current_dir, folder, file))
            print(f"Moved {file} to {folder} folder")
Enter fullscreen mode Exit fullscreen mode

This script will organize your files into different folders based on their extensions. You can customize the folders and extensions to suit your needs.

Script 2: Automatic Email Sender

Another useful script is an automatic email sender. This script can help you send emails to multiple recipients with a single click. Here is an example of how you can create an automatic email sender script:

import smtplib
from email.mime.text import MIMEText

# Define the email parameters
sender_email = "your_email@gmail.com"
sender_password = "your_password"
recipient_emails = ["recipient1@example.com", "recipient2@example.com"]
subject = "Test Email"
body = "This is a test email sent using Python"

# Create a text message
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = ', '.join(recipient_emails)

# Send the email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_emails, msg.as_string())
server.quit()
print("Email sent successfully")
Enter fullscreen mode Exit fullscreen mode

This script will send an email to multiple recipients with a single click. You can customize the email parameters to suit your needs.

Script 3: Website Monitor

A website monitor script can help you monitor the status of a website and send you an email if it goes down. Here is an example of how you can create a website monitor script:

import requests
import time
from email.mime.text import MIMEText
import smtplib

# Define the website URL and email parameters
url = "http://example.com"
sender_email = "your_email@gmail.com"
sender_password = "your_password"
recipient_email = "recipient@example.com"

# Loop indefinitely
while True:
    try:
        # Send a GET request to the website
        response = requests.get(url)
        # Check if the website is up
        if response.status_code == 200:
            print("Website is up")
        else:
            # Send an email if the website is down
            msg = MIMEText(f"Website {url} is down")
            msg['Subject'] = "Website Down"
            msg['From'] = sender_email
            msg['To'] = recipient_email
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.starttls()
            server.login(sender_email, sender_password)
            server.sendmail(sender_email, recipient_email, msg.as_string())
            server.quit()
            print("Email sent")
    except requests.exceptions.RequestException:
        # Send an email if there is a network error
        msg = MIMEText(f"Network error occurred while checking website {url}")
        msg['Subject'] = "Network Error"
        msg['From'] = sender_email
        msg['To'] = recipient_email
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(sender_email, sender_password)
        server.sendmail(sender_email, recipient_email, msg.as_string())
        server.quit()
        print("Email sent")
    # Wait for 1 minute before checking again
    time.sleep(60)
Enter fullscreen mode Exit fullscreen mode

This script will monitor the status of a website and send you an email if it goes down. You can customize the website URL and email parameters to suit your needs.

Script 4: Data Entry Automation

A data entry automation script can help you automate data entry tasks by reading data from a CSV file and entering it into a website or application. Here is an example of how you can create a data entry automation script:


python
import csv
import pyautogui

# Open the CSV file
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    # Loop through each row in the CSV file
    for row in reader:
        # Enter the data into

---

### 🛠️ Recommended Tool

If you found this useful, check out **[Content Creator Ultimate Bundle (Save 33%)](https://gumroad.com)** — $29.99 and designed for developers like you.

*Get instant access to our best-selling AI Dev Boost, HTML Landing Page Templates, AI Prompts for Developers, and Python Automation Scripts Pack, perfect for content creators and marketers looking to elevate their game. This bundle is a must-have for anyone looking to create stunning content, build high-converting landing pages, and drive real results. With these tools, you'll be able to create engaging content, build beautiful landing pages, and boost your online presence.*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)