python 常用自动化脚本,值得收藏!!!

1. 批量文件重命名

import os
def batch_rename(path, prefix='', suffix=''):
    for i, filename in enumerate(os.listdir(path)):
        new_name = f"{prefix}{i:03d}{suffix}{os.path.splitext(filename)[1]}"
        old_file = os.path.join(path, filename)
        new_file = os.path.join(path, new_name)
        os.rename(old_file, new_file)

# 使用示例:
batch_rename('/path/to/your/directory', 'file_', '.txt')

2. 自动邮件发送

import smtplib
from email.mime.text import MIMEText

def send_email(to_addr, subject, content):
    smtp_server = 'smtp.example.com'
    username = 'your-email@example.com'
    password = 'your-password'

    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = username
    msg['To'] = to_addr

    server = smtplib.SMTP(smtp_server, 587)
    server.starttls()
    server.login(username, password)
    server.sendmail(username, to_addr, msg.as_string())
    server.quit()

# 使用示例:
send_email('receiver@example.com', '每日提醒报告', '报告已生成,请查收....')

3. 数据库操作自动化

import sqlite3

def create_connection(dabase_file):
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(f"成功连接到数据库:{dabase_file}")
    except Error as e:
        print(e)

    return conn

def insert_data(conn, table_name, data_dict):
    keys = ', '.join(data_dict.keys())
    values = ', '.join(f"'{v}'" for v in data_dict.values())

    sql = f"INSERT INTO {table_name} ({keys}) VALUES ({values});"
    try:
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()
        print("insert 成功!")
    except sqlite3.Error as e:
        print(e)

# 使用示例:
conn = create_connection('my_database.db')
data = {'name': 'jiang', 'age': 32}
insert_data(conn, 'users', data)

# 在适当时候关闭数据库连接
conn.close()

4. 定时任务执行

import schedule
import time

def job_to_schedule():
    print("当前时间:", time.ctime(), "任务正在执行中")

# 定义每天11点执行任务
schedule.every().day.at("11:00").do(job_to_schedule)

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

5.网页内容自动化抓取

import requests
from bs4 import BeautifulSoup

def fetch_web_content(url):
    res = requests.get(url)
    if res.status_code == 200:
        soup = BeautifulSoup(res.text, 'html.parser')
        # 示例提取页面标题
        title = soup.find('title').text
        return title
    else:
        return "无法获取网页内容"

# 使用示例:
url = 'https://xxxx.com'
web_title = fetch_web_content(url)
print("网页标题:", web_title)

6. 数据清洗自动化

import pandas as pd

def clean_data(file_path):
    df = pd.read_csv(file_path)
    
    # 示例:处理缺失值
    df.fillna('N/A', inplace=True)

    # 示例:去除重复行
    df.drop_duplicates(inplace=True)

    # 示例:转换列类型
    df['date_column'] = pd.to_datetime(df['date_column'])

    return df

# 使用示例:处理 data.csv 文件
cleaned_df = clean_data('data.csv')
print("数据清洗完成!")

7. 图片批量压缩

from PIL import Image
import os

def compress_images(dir_path, quality=90):
    for filename in os.listdir(dir_path):
        if filename.endswith(".jpg") or filename.endswith(".png"):
            img = Image.open(os.path.join(dir_path, filename))
            img.save(os.path.join(dir_path, f'compressed_{filename}'), optimize=True, quality=quality)

# 使用示例:
compress_images('/path/to/images', quality=80)

8. 文件内容查找替换

import fileinput

def search_replace_in_files(dir_path, search_text, replace_text):
    for line in fileinput.input([f"{dir_path}/*"], inplace=True):
        print(line.replace(search_text, replace_text), end='')

# 使用示例:
search_replace_in_files('/path/to/files', 'old_text', 'new_text')

9.日志文件分析

def analyze_log(logfile):
    with open(logfile, 'r') as f:
        lines = f.readlines()

    error_count = 0
    for line in lines:
        if "ERROR" in line:
            error_count += 1

    print(f"日志文件中包含 {error_count} 条错误记录。")

# 使用示例:
analyze_log('application.log')

10. 邮件附件批量下载

import imaplib
import email
from email.header import decode_header
import os

def download_attachments(email_addr, password, imap_server, folder='INBOX'):
    mail = imaplib.IMAP4_SSL(imap_server)
    mail.login(email_addr, password)

    mail.select(folder)
    result, data = mail.uid('search', None, "ALL")
    uids = data[0].split()

    for uid in uids:
        _, msg_data = mail.uid('fetch', uid, '(RFC822)')
        raw_email = msg_data[0][1].decode("utf-8")
        email_message = email.message_from_string(raw_email)

        for part in email_message.walk():
            if part.get_content_maintype() == 'multipart':
                continue
            if part.get('Content-Disposition') is None:
                continue
            
            filename = part.get_filename()
            if bool(filename):
                file_data = part.get_payload(decode=True)
                with open(os.path.join('/path/to/download', filename), 'wb') as f:
                    f.write(file_data)

    mail.close()
    mail.logout()

# 使用示例:
download_attachments('youremail@example.com', 'yourpassword', 'imap.example.com')

11. 定时发送报告自动化

import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def generate_report(source, to_addr, subject):
    # 假设这里是从数据库或文件中获取数据并生成报告内容
    report_content = pd.DataFrame({"Data": [1, 2, 3], "Info": ["A", "B", "C"]}).to_html()

    msg = MIMEMultipart()
    msg['From'] = 'your-email@example.com'
    msg['To'] = to_addr
    msg['Subject'] = subject

    msg.attach(MIMEText(report_content, 'html'))

    server = smtplib.SMTP('smtp.example.com', 587)
    server.starttls()
    server.login('your-email@example.com', 'your-password')
    text = msg.as_string()
    server.sendmail('your-email@example.com', to_addr, text)
    server.quit()

# 使用示例:
generate_report('data.csv', '目标邮件@example.com', '每日数据报告')

12. locust 自动化性能测试

from locust import HttpUser, task, between

class WebsiteUser(HttpUser):
    wait_time = between(5, 15)  # 定义用户操作之间的等待时间

    @task
    def load_test_api(self):
        response = self.client.get("/api/data")
        assert response.status_code == 200  # 验证返回状态码为200

    @task(3)  # 指定该任务在总任务中的执行频率是其他任务的3倍
    def post_data(self):
        data = {"key": "value"}
        response = self.client.post("/api/submit", json=data)
        assert response.status_code == 201  # 验证数据成功提交后的响应状态码

# 运行Locust命令启动性能测试:
# locust -f your_test_script.py --host=http://your-api-url.com

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值