Python自动化脚本:告别重复劳动

你是否曾经花费数小时重复做着相同的文件整理、数据录入或系统维护工作?想象一下,如果这些繁琐任务都能自动完成,你的工作效率将提升多少倍!Python自动化脚本正是为此而生,它就像雇佣了一个不知疲倦的数字助手,帮你处理各种重复性工作。

实战代码:构建个人自动化工具箱

文件批量处理脚本

python 复制代码
import os
import shutil
from datetime import datetime

def organize_files_by_extension(folder_path):
    """
    按文件扩展名自动整理文件夹
    """
    # 确保目标文件夹存在
    if not os.path.exists(folder_path):
        print(f"错误:文件夹 {folder_path} 不存在")
        return
    
    # 定义文件类型分类
    file_categories = {
        '图片': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
        '文档': ['.pdf', '.doc', '.docx', '.txt', '.xlsx', '.pptx'],
        '视频': ['.mp4', '.avi', '.mov', '.mkv'],
        '音频': ['.mp3', '.wav', '.flac'],
        '代码': ['.py', '.js', '.html', '.css', '.java'],
        '压缩包': ['.zip', '.rar', '.7z']
    }
    
    # 创建分类文件夹
    for category in file_categories.keys():
        category_path = os.path.join(folder_path, category)
        if not os.path.exists(category_path):
            os.makedirs(category_path)
    
    # 遍历并整理文件
    moved_files = 0
    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)
        
        # 跳过文件夹和隐藏文件
        if os.path.isdir(file_path) or filename.startswith('.'):
            continue
        
        # 获取文件扩展名
        _, file_extension = os.path.splitext(filename)
        file_extension = file_extension.lower()
        
        # 查找对应的文件分类
        moved = False
        for category, extensions in file_categories.items():
            if file_extension in extensions:
                target_path = os.path.join(folder_path, category, filename)
                shutil.move(file_path, target_path)
                print(f"移动: {filename} -> {category}/")
                moved_files += 1
                moved = True
                break
        
        # 未分类的文件移到"其他"文件夹
        if not moved:
            other_path = os.path.join(folder_path, "其他")
            if not os.path.exists(other_path):
                os.makedirs(other_path)
            shutil.move(file_path, os.path.join(other_path, filename))
            print(f"移动: {filename} -> 其他/")
            moved_files += 1
    
    print(f"\n整理完成!共移动了 {moved_files} 个文件")

# 使用示例
organize_files_by_extension("/Users/你的用户名/Downloads")

自动备份脚本

python 复制代码
import os
import shutil
import schedule
import time
from datetime import datetime

def create_backup(source_dir, backup_dir):
    """
    创建文件备份
    """
    try:
        # 创建带时间戳的备份文件夹
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        backup_path = os.path.join(backup_dir, f"backup_{timestamp}")
        
        # 复制文件
        if os.path.exists(source_dir):
            shutil.copytree(source_dir, backup_path)
            print(f"备份成功: {backup_path}")
            return backup_path
        else:
            print(f"错误:源目录 {source_dir} 不存在")
            return None
            
    except Exception as e:
        print(f"备份失败: {e}")
        return None

def cleanup_old_backups(backup_dir, keep_days=7):
    """
    清理旧的备份文件
    """
    current_time = time.time()
    deleted_count = 0
    
    for item in os.listdir(backup_dir):
        item_path = os.path.join(backup_dir, item)
        
        # 检查是否是备份文件夹且超过保留期限
        if os.path.isdir(item_path) and item.startswith("backup_"):
            # 获取文件夹创建时间
            creation_time = os.path.getctime(item_path)
            age_days = (current_time - creation_time) / (24 * 3600)
            
            if age_days > keep_days:
                shutil.rmtree(item_path)
                print(f"删除旧备份: {item}")
                deleted_count += 1
    
    print(f"清理完成!删除了 {deleted_count} 个旧备份")

def setup_auto_backup(source_dir, backup_dir, interval_hours=24):
    """
    设置自动备份任务
    """
    def backup_job():
        print(f"\n执行自动备份 - {datetime.now()}")
        create_backup(source_dir, backup_dir)
        cleanup_old_backups(backup_dir)
    
    # 设置定时任务
    schedule.every(interval_hours).hours.do(backup_job)
    
    print(f"自动备份已设置:每{interval_hours}小时备份一次")
    print(f"源目录: {source_dir}")
    print(f"备份目录: {backup_dir}")
    
    # 立即执行一次备份
    backup_job()
    
    # 保持程序运行(在实际使用中可能需要用系统服务代替)
    try:
        while True:
            schedule.run_pending()
            time.sleep(60)  # 每分钟检查一次
    except KeyboardInterrupt:
        print("\n自动备份已停止")

# 使用示例(取消注释来运行)
# setup_auto_backup(
#     source_dir="/Users/你的用户名/重要文档",
#     backup_dir="/Users/你的用户名/文档备份",
#     interval_hours=24
# )

网页内容监控脚本

python 复制代码
import requests
import time
import smtplib
from email.mime.text import MIMEText
from datetime import datetime

class WebsiteMonitor:
    def __init__(self, websites, check_interval=300):
        """
        初始化网站监控器
        websites: 要监控的网站列表 [{'name': '网站名', 'url': '网址'}]
        check_interval: 检查间隔(秒)
        """
        self.websites = websites
        self.check_interval = check_interval
        self.status_history = {}
    
    def check_website_status(self, url):
        """
        检查网站状态
        """
        try:
            response = requests.get(url, timeout=10)
            return response.status_code == 200, response.status_code
        except requests.exceptions.RequestException as e:
            return False, str(e)
    
    def send_alert(self, website_name, status, message):
        """
        发送警报(这里打印到控制台,实际使用时可以发送邮件)
        """
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        alert_message = f"[{timestamp}] 警报: {website_name} - {status}\n详情: {message}"
        print(alert_message)
        
        # 在实际使用中,这里可以添加邮件发送代码
        # self.send_email_alert(website_name, status, message)
    
    def monitor_websites(self):
        """
        开始监控网站
        """
        print(f"开始监控 {len(self.websites)} 个网站...")
        
        try:
            while True:
                for website in self.websites:
                    name = website['name']
                    url = website['url']
                    
                    # 检查网站状态
                    is_online, status = self.check_website_status(url)
                    current_status = "在线" if is_online else "离线"
                    
                    # 检查状态变化
                    previous_status = self.status_history.get(name)
                    if previous_status != current_status:
                        if previous_status is not None:  # 不是第一次检查
                            self.send_alert(name, current_status, f"状态码: {status}")
                        
                        self.status_history[name] = current_status
                    
                    # 输出状态信息
                    status_icon = "✅" if is_online else "❌"
                    print(f"{status_icon} {name}: {current_status} ({status})")
                
                print(f"等待 {self.check_interval} 秒后再次检查...\n")
                time.sleep(self.check_interval)
                
        except KeyboardInterrupt:
            print("\n网站监控已停止")

# 使用示例
websites_to_monitor = [
    {'name': '谷歌', 'url': 'https://www.google.com'},
    {'name': 'GitHub', 'url': 'https://www.github.com'},
    {'name': '稀土掘金', 'url': 'https://juejin.cn'}
]

monitor = WebsiteMonitor(websites_to_monitor, check_interval=60)
# monitor.monitor_websites()  # 取消注释来运行

自动化数据收集脚本

pythonimport 复制代码
import json
import csv
import sqlite3
from datetime import datetime

class DataCollector:
    def __init__(self, db_path="collected_data.db"):
        self.db_path = db_path
        self.setup_database()
    
    def setup_database(self):
        """设置SQLite数据库"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        # 创建数据表
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS weather_data (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                city TEXT NOT NULL,
                temperature REAL,
                humidity INTEGER,
                description TEXT,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        ''')
        
        conn.commit()
        conn.close()
        print("数据库初始化完成")
    
    def get_weather_data(self, city):
        """获取天气数据(模拟API调用)"""
        # 这里使用模拟数据,实际使用时可以调用真实天气API
        weather_data = {
            '北京': {'temperature': 25.5, 'humidity': 60, 'description': '晴朗'},
            '上海': {'temperature': 28.0, 'humidity': 75, 'description': '多云'},
            '广州': {'temperature': 32.5, 'humidity': 80, 'description': '阵雨'},
            '深圳': {'temperature': 31.0, 'humidity': 78, 'description': '阴天'}
        }
        
        return weather_data.get(city, {'temperature': 0, 'humidity': 0, 'description': '未知'})
    
    def collect_weather_data(self, cities):
        """收集多个城市的天气数据"""
        collected_data = []
        
        for city in cities:
            print(f"正在收集 {city} 的天气数据...")
            weather = self.get_weather_data(city)
            
            data_point = {
                'city': city,
                'temperature': weather['temperature'],
                'humidity': weather['humidity'],
                'description': weather['description'],
                'timestamp': datetime.now()
            }
            
            collected_data.append(data_point)
            
            # 保存到数据库
            self.save_to_database(data_point)
        
        return collected_data
    
    def save_to_database(self, data):
        """保存数据到数据库"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        cursor.execute('''
            INSERT INTO weather_data (city, temperature, humidity, description)
            VALUES (?, ?, ?, ?)
        ''', (data['city'], data['temperature'], data['humidity'], data['description']))
        
        conn.commit()
        conn.close()
    
    def export_to_csv(self, filename="weather_data.csv"):
        """导出数据到CSV文件"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        cursor.execute('SELECT * FROM weather_data')
        data = cursor.fetchall()
        
        with open(filename, 'w', newline='', encoding='utf-8') as file:
            writer = csv.writer(file)
            
            # 写入表头
            writer.writerow(['ID', '城市', '温度', '湿度', '描述', '时间'])
            
            # 写入数据
            for row in data:
                writer.writerow(row)
        
        conn.close()
        print(f"数据已导出到 {filename}")
    
    def generate_report(self):
        """生成数据报告"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        # 统计各城市平均温度
        cursor.execute('''
            SELECT city, 
                   AVG(temperature) as avg_temp,
                   AVG(humidity) as avg_humidity,
                   COUNT(*) as data_count
            FROM weather_data 
            GROUP BY city
        ''')
        
        stats = cursor.fetchall()
        
        print("\n=== 天气数据统计报告 ===")
        for city, avg_temp, avg_humidity, count in stats:
            print(f"{city}:")
            print(f"  平均温度: {avg_temp:.1f}°C")
            print(f"  平均湿度: {avg_humidity:.1f}%")
            print(f"  数据点数: {count}")
            print()
        
        conn.close()

# 使用示例
collector = DataCollector()

# 收集数据
cities = ['北京', '上海', '广州', '深圳']
weather_data = collector.collect_weather_data(cities)

print("收集到的天气数据:")
for data in weather_data:
    print(f"{data['city']}: {data['temperature']}°C, {data['humidity']}%, {data['description']}")

# 生成报告和导出数据
collector.generate_report()
collector.export_to_csv()
import requests
import json
import csv
import sqlite3
from datetime import datetime

class DataCollector:
    def __init__(self, db_path="collected_data.db"):
        self.db_path = db_path
        self.setup_database()
    
    def setup_database(self):
        """设置SQLite数据库"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        # 创建数据表
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS weather_data (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                city TEXT NOT NULL,
                temperature REAL,
                humidity INTEGER,
                description TEXT,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        ''')
        
        conn.commit()
        conn.close()
        print("数据库初始化完成")
    
    def get_weather_data(self, city):
        """获取天气数据(模拟API调用)"""
        # 这里使用模拟数据,实际使用时可以调用真实天气API
        weather_data = {
            '北京': {'temperature': 25.5, 'humidity': 60, 'description': '晴朗'},
            '上海': {'temperature': 28.0, 'humidity': 75, 'description': '多云'},
            '广州': {'temperature': 32.5, 'humidity': 80, 'description': '阵雨'},
            '深圳': {'temperature': 31.0, 'humidity': 78, 'description': '阴天'}
        }
        
        return weather_data.get(city, {'temperature': 0, 'humidity': 0, 'description': '未知'})
    
    def collect_weather_data(self, cities):
        """收集多个城市的天气数据"""
        collected_data = []
        
        for city in cities:
            print(f"正在收集 {city} 的天气数据...")
            weather = self.get_weather_data(city)
            
            data_point = {
                'city': city,
                'temperature': weather['temperature'],
                'humidity': weather['humidity'],
                'description': weather['description'],
                'timestamp': datetime.now()
            }
            
            collected_data.append(data_point)
            
            # 保存到数据库
            self.save_to_database(data_point)
        
        return collected_data
    
    def save_to_database(self, data):
        """保存数据到数据库"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        cursor.execute('''
            INSERT INTO weather_data (city, temperature, humidity, description)
            VALUES (?, ?, ?, ?)
        ''', (data['city'], data['temperature'], data['humidity'], data['description']))
        
        conn.commit()
        conn.close()
    
    def export_to_csv(self, filename="weather_data.csv"):
        """导出数据到CSV文件"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        cursor.execute('SELECT * FROM weather_data')
        data = cursor.fetchall()
        
        with open(filename, 'w', newline='', encoding='utf-8') as file:
            writer = csv.writer(file)
            
            # 写入表头
            writer.writerow(['ID', '城市', '温度', '湿度', '描述', '时间'])
            
            # 写入数据
            for row in data:
                writer.writerow(row)
        
        conn.close()
        print(f"数据已导出到 {filename}")
    
    def generate_report(self):
        """生成数据报告"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        # 统计各城市平均温度
        cursor.execute('''
            SELECT city, 
                   AVG(temperature) as avg_temp,
                   AVG(humidity) as avg_humidity,
                   COUNT(*) as data_count
            FROM weather_data 
            GROUP BY city
        ''')
        
        stats = cursor.fetchall()
        
        print("\n=== 天气数据统计报告 ===")
        for city, avg_temp, avg_humidity, count in stats:
            print(f"{city}:")
            print(f"  平均温度: {avg_temp:.1f}°C")
            print(f"  平均湿度: {avg_humidity:.1f}%")
            print(f"  数据点数: {count}")
            print()
        
        conn.close()

# 使用示例
collector = DataCollector()

# 收集数据
cities = ['北京', '上海', '广州', '深圳']
weather_data = collector.collect_weather_data(cities)

print("收集到的天气数据:")
for data in weather_data:
    print(f"{data['city']}: {data['temperature']}°C, {data['humidity']}%, {data['description']}")

# 生成报告和导出数据
collector.generate_report()
collector.export_to_csv()

自动化脚本开发原则

  1. 可靠性设计
  2. 用户体验
  3. 维护性考虑
  4. 安全性保障
相关推荐
BBB努力学习程序设计1 小时前
Python函数式编程:优雅的代码艺术
python·pycharm
2501_940943912 小时前
体系课\ Python Web全栈工程师
开发语言·前端·python
田姐姐tmner2 小时前
Python切片
开发语言·python
t***31652 小时前
爬虫学习案例3
爬虫·python·学习
AI小云3 小时前
【数据操作与可视化】Pandas数据处理-其他操作
python·pandas
大佬,救命!!!3 小时前
更换适配python版本直接进行机器学习深度学习等相关环境配置(非仿真环境)
人工智能·python·深度学习·机器学习·学习笔记·详细配置
无心水4 小时前
【Python实战进阶】4、Python字典与集合深度解析
开发语言·人工智能·python·python字典·python集合·python实战进阶·python工业化实战进阶
上班职业摸鱼人4 小时前
python文件中导入另外一个模块这个模块
python
永远是夏天4 小时前
Python面向对象编程(OOP)全教程:从入门到实战(附案例)
python