Operations 7 min read

10 Practical Python Automation Scripts for File Management, Web Scraping, Data Cleaning, and More

This article presents ten useful Python automation scripts covering file renaming, web page downloading, data cleaning, scheduled tasks, email sending, testing, database backup, log analysis, file compression, and document generation, each with clear explanations and ready‑to‑run code examples.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
10 Practical Python Automation Scripts for File Management, Web Scraping, Data Cleaning, and More

Introduction In the programmer's world, automation is the key to efficiency. Python, with its readability and extensive libraries, is the preferred language for automating tasks. This article curates ten practical Python automation scripts spanning file handling, network requests, data analysis, and more, aiming to save time and reduce repetitive work.

Theoretical Foundations Python automation relies on the standard library and third‑party packages such as os , requests , pandas , etc., enabling easy file operations, data fetching, and analysis.

Scenario 1: Batch Rename Files When dealing with large numbers of media files, a consistent naming convention is essential.

import os

def batch_rename(directory, prefix):
    i = 1
    for filename in os.listdir(directory):
        ext = os.path.splitext(filename)[1]
        new_name = f"{prefix}_{i}{ext}"
        os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
        i += 1

batch_rename('/path/to/directory', 'file')

Scenario 2: Automatic Webpage Download For researchers or data analysts, automatically fetching web data is crucial.

import requests

def download_webpage(url, output_file):
    response = requests.get(url)
    with open(output_file, 'w') as file:
        file.write(response.text)

download_webpage('http://example.com', 'webpage.html')

Scenario 3: Data Cleaning Data preprocessing is a vital step in data analysis.

import pandas as pd

def clean_data(file_path):
    df = pd.read_csv(file_path)
    df.dropna(inplace=True)
    df.to_csv(file_path, index=False)

clean_data('data.csv')

Scenario 4: Scheduled Task Execution Ensures tasks run at specific times, such as sending daily reports.

import schedule
import time

def job():
    print("Task executed")

schedule.every().day.at("10:30").do(job)

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

Scenario 5: Automated Email Sending Send reports automatically without manual effort.

import smtplib
from email.mime.text import MIMEText

def send_email(subject, message, to_email):
    msg = MIMEText(message)
    msg['Subject'] = subject
    msg['From'] = '[email protected]'
    msg['To'] = to_email
    s = smtplib.SMTP('localhost')
    s.sendmail('[email protected]', [to_email], msg.as_string())
    s.quit()

send_email('Daily Report', 'Here is your daily report.', '[email protected]')

Scenario 6: Automated Testing Ensures code quality by running tests after each change.

import unittest

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

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

Scenario 7: Database Operations Automates database backup and restoration.

import sqlite3

def backup_database(db_path, backup_path):
    conn = sqlite3.connect(db_path)
    backup_conn = sqlite3.connect(backup_path)
    conn.backup(backup_conn)
    backup_conn.close()
    conn.close()

backup_database('database.db', 'backup.db')

Scenario 8: Log Analysis Extracts key information from log files.

import re

def analyze_logs(log_file):
    pattern = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
    with open(log_file, 'r') as file:
        for line in file:
            match = re.search(pattern, line)
            if match:
                print(match.group())

analyze_logs('access.log')

Scenario 9: File Compression Batch compress files for easier storage and transfer.

import zipfile

def compress_files(file_paths, zip_file):
    with zipfile.ZipFile(zip_file, 'w') as myzip:
        for file_path in file_paths:
            myzip.write(file_path)

compress_files(['file1.txt', 'file2.txt'], 'archive.zip')

Scenario 10: Automated Document Generation Generate reports automatically from data.

from docx import Document

def generate_report(data, report_file):
    doc = Document()
    doc.add_heading('Report', 0)
    doc.add_paragraph(str(data))
    doc.save(report_file)

generate_report('Sample data', 'report.docx')

Conclusion These ten Python automation scripts demonstrate Python's powerful capabilities in automating everyday tasks and professional development workflows. By integrating these scripts into your processes, you can boost efficiency, reduce manual effort, and enjoy the benefits of automation.

automationoperationsscriptingCodeExamples
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.