Operations 12 min read

Comprehensive Python Automation Scripts for Common Tasks

This article presents a collection of practical Python scripts covering file management, web scraping, email sending, Excel handling, data cleaning, image processing, system monitoring, PDF manipulation, OCR, database interaction, social media posting, testing, and cloud storage, each with clear descriptions and ready‑to‑run code examples.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Comprehensive Python Automation Scripts for Common Tasks

1. File Management Automation

Scripts for organizing files by extension, removing empty folders, and batch‑renaming files.

import os
from shutil import move

def sort_files(directory_path):
    for filename in os.listdir(directory_path):
        if os.path.isfile(os.path.join(directory_path, filename)):
            file_extension = filename.split('.')[-1]
            destination_directory = os.path.join(directory_path, file_extension)
            if not os.path.exists(destination_directory):
                os.makedirs(destination_directory)
            move(os.path.join(directory_path, filename), os.path.join(destination_directory, filename))
# 示例
sort_files('/path/to/directory')
import os

def remove_empty_folders(directory_path):
    for root, dirs, files in os.walk(directory_path, topdown=False):
        for folder in dirs:
            folder_path = os.path.join(root, folder)
            if not os.listdir(folder_path):
                os.rmdir(folder_path)
# 示例
remove_empty_folders('/path/to/directory')
import os

def rename_files(directory_path, old_name, new_name):
    for filename in os.listdir(directory_path):
        if old_name in filename:
            new_filename = filename.replace(old_name, new_name)
            os.rename(os.path.join(directory_path, filename), os.path.join(directory_path, new_filename))
# 示例
rename_files('/path/to/directory', 'old', 'new')

2. Web Scraping

Functions to extract data from websites and download images in bulk.

import requests
from bs4 import BeautifulSoup

def scrape_data(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    # 在此编写提取逻辑
    return soup
# 示例
url = 'https://example.com'
soup = scrape_data(url)
print(soup.title.string)
import requests

def download_images(url, save_directory):
    response = requests.get(url)
    if response.status_code == 200:
        images = response.json()  # 假设返回图片 URL 列表
        for index, image_url in enumerate(images):
            image_response = requests.get(image_url)
            if image_response.status_code == 200:
                with open(f"{save_directory}/image_{index}.jpg", "wb") as f:
                    f.write(image_response.content)
# 示例
download_images('https://api.example.com/images', '/path/to/save')

3. File Processing and Operations

Utilities for counting words in a text file and performing find‑replace operations.

def count_words(file_path):
    with open(file_path, 'r') as f:
        text = f.read()
    word_count = len(text.split())
    return word_count
# 示例
word_count = count_words('/path/to/file.txt')
print(f"Word count: {word_count}")
def find_replace(file_path, search_text, replace_text):
    with open(file_path, 'r') as f:
        text = f.read()
    modified_text = text.replace(search_text, replace_text)
    with open(file_path, 'w') as f:
        f.write(modified_text)
# 示例
find_replace('/path/to/file.txt', 'old', 'new')

4. Email Automation

Send personalized emails using SMTP.

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_personalized_email(sender_email, sender_password, recipients, subject, body):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(sender_email, sender_password)
    for recipient_email in recipients:
        message = MIMEMultipart()
        message['From'] = sender_email
        message['To'] = recipient_email
        message['Subject'] = subject
        message.attach(MIMEText(body, 'plain'))
        server.send_message(message)
    server.quit()
# 示例
sender_email = '[email protected]'
sender_password = 'your_password'
recipients = ['[email protected]', '[email protected]']
subject = 'Hello'
body = 'This is a test email.'
send_personalized_email(sender_email, sender_password, recipients, subject, body)

5. Excel Spreadsheet Automation

Read from and write to Excel files using pandas.

import pandas as pd

def read_excel(file_path):
    df = pd.read_excel(file_path)
    return df

def write_to_excel(data, file_path):
    df = pd.DataFrame(data)
    df.to_excel(file_path, index=False)
# 示例
data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]}
write_to_excel(data, '/path/to/output.xlsx')
df = read_excel('/path/to/output.xlsx')
print(df)

6. Data Cleaning and Transformation

Remove duplicate rows from an Excel dataset.

import pandas as pd

def remove_duplicates(file_path):
    df = pd.read_excel(file_path)
    df.drop_duplicates(inplace=True)
    df.to_excel(file_path, index=False)
# 示例
remove_duplicates('/path/to/data.xlsx')

7. Image Editing Automation

Resize images with Pillow.

from PIL import Image

def resize_image(input_path, output_path, width, height):
    image = Image.open(input_path)
    resized_image = image.resize((width, height), Image.ANTIALIAS)
    resized_image.save(output_path)
# 示例
resize_image('/path/to/input.jpg', '/path/to/output.jpg', 800, 600)

8. System Task Automation

Monitor disk space and warn when below a threshold.

import shutil

def check_disk_space(path, threshold):
    total, used, free = shutil.disk_usage(path)
    free_gb = free // (2**30)
    if free_gb < threshold:
        print(f"Warning: Free disk space is below {threshold} GB.")
    else:
        print(f"Free disk space: {free_gb} GB.")
# 示例
check_disk_space('/', 10)

9. Network Automation

Check website status.

import requests

def check_website_status(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            print(f"Website {url} is up and running.")
        else:
            print(f"Website {url} returned status code {response.status_code}.")
    except requests.exceptions.RequestException as e:
        print(f"Error accessing website {url}: {e}")
# 示例
check_website_status('https://example.com')

10. PDF Operations Automation

Extract text from PDF files using PyPDF2.

import PyPDF2

def extract_text_from_pdf(pdf_path):
    with open(pdf_path, 'rb') as file:
        reader = PyPDF2.PdfFileReader(file)
        text = ''
        for page_num in range(reader.numPages):
            page = reader.getPage(page_num)
            text += page.extractText()
    return text
# 示例
text = extract_text_from_pdf('/path/to/document.pdf')
print(text)

11. OCR Recognition

Recognize text in images using Tesseract.

import pytesseract
from PIL import Image

def recognize_text(image_path):
    image = Image.open(image_path)
    text = pytesseract.image_to_string(image,)  # 使用简体中文
    return text
# 示例
text = recognize_text('/path/to/image.jpg')
print(text)

12. Database Interaction

Connect to a SQLite database and execute queries.

import sqlite3

def connect_to_database(db_path):
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    return conn, cursor

def execute_query(cursor, query):
    cursor.execute(query)
    results = cursor.fetchall()
    return results
# 示例
conn, cursor = connect_to_database('/path/to/database.db')
query = 'SELECT * FROM table_name'
results = execute_query(cursor, query)
print(results)
conn.close()

13. Social Media Automation

Post a tweet using Tweepy.

import tweepy

def post_tweet(api_key, api_secret, access_token, access_token_secret, message):
    auth = tweepy.OAuthHandler(api_key, api_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    api.update_status(message)
# 示例
api_key = 'your_api_key'
api_secret = 'your_api_secret'
access_token = 'your_access_token'
access_token_secret = 'your_access_token_secret'
message = 'Hello, Twitter!'
post_tweet(api_key, api_secret, access_token, access_token_secret, message)

14. Test Automation

Unit testing with unittest.

import unittest

class TestMyFunction(unittest.TestCase):
    def test_addition(self):
        result = add(1, 2)
        self.assertEqual(result, 3)

def add(a, b):
    return a + b

if __name__ == '__main__':
    unittest.main()

15. Cloud Service Automation

Upload files to AWS S3.

import boto3

def upload_to_s3(bucket_name, file_path, object_name):
    s3 = boto3.client('s3')
    s3.upload_file(file_path, bucket_name, object_name)
# 示例
bucket_name = 'your-bucket-name'
file_path = '/path/to/file.txt'
object_name = 'file.txt'
upload_to_s3(bucket_name, file_path, object_name)

These scripts constitute a practical toolbox for automating everyday tasks with Python, helping to boost productivity and reduce repetitive manual work.

PythonautomationData ProcessingscriptingWeb Scrapingfile management
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.