14個 Python 自動化實戰(zhàn)腳本

1.批量文件重命名神器在工作中,我們常常需要對大量文件進(jìn)行批量重命名,Python幫你輕松搞定!
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.自動發(fā)送郵件通知告別手動發(fā)送,用Python編寫定時發(fā)送郵件的自動化腳本。
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.定時任務(wù)自動化執(zhí)行使用Python調(diào)度庫,實現(xiàn)定時執(zhí)行任務(wù)的自動化腳本。
import schedule
import time
def job_to_schedule():
    print("當(dāng)前時間:", time.ctime(), "任務(wù)正在執(zhí)行...")
# 定義每天9點執(zhí)行任務(wù)
schedule.every().day.at("09:00").do(job_to_schedule)
while True:
    schedule.run_pending()
    time.sleep(1)
# 使用示例:
# 運行此腳本后,每天上午9點會自動打印當(dāng)前時間及提示信息4.數(shù)據(jù)庫操作自動化簡化數(shù)據(jù)庫管理,Python幫你自動化執(zhí)行CRUD操作。
import sqlite3
def create_connection(db_file):
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(f"成功連接到SQLite數(shù)據(jù)庫:{db_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("數(shù)據(jù)插入成功!")
    except sqlite3.Error as e:
        print(e)
# 使用示例:
conn = create_connection('my_database.db')
data = {'name': 'John Doe', 'age': 30}
insert_data(conn, 'users', data)
# 在適當(dāng)時候關(guān)閉數(shù)據(jù)庫連接
conn.close()5.網(wǎng)頁內(nèi)容自動化抓取利用BeautifulSoup和requests庫,編寫Python爬蟲獲取所需網(wǎng)頁信息。
import requests
from bs4 import BeautifulSoup
def fetch_web_content(url):
    response = requests.get(url)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        # 示例提取頁面標(biāo)題
        title = soup.find('title').text
        return title
    else:
        return "無法獲取網(wǎng)頁內(nèi)容"
# 使用示例:
url = 'https://example.com'
web_title = fetch_web_content(url)
print("網(wǎng)頁標(biāo)題:", web_title)6.數(shù)據(jù)清洗自動化使用Pandas庫,實現(xiàn)復(fù)雜數(shù)據(jù)處理和清洗的自動化。
import pandas as pd
def clean_data(file_path):
    df = pd.read_csv(file_path)
    
    # 示例:處理缺失值
    df.fillna('N/A', inplace=True)
    # 示例:去除重復(fù)行
    df.drop_duplicates(inplace=True)
    # 示例:轉(zhuǎn)換列類型
    df['date_column'] = pd.to_datetime(df['date_column'])
    return df
# 使用示例:
cleaned_df = clean_data('data.csv')
print("數(shù)據(jù)清洗完成,已準(zhǔn)備就緒!")7.圖片批量壓縮用Python快速壓縮大量圖片以節(jié)省存儲空間。
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.文件內(nèi)容查找替換Python腳本幫助你一鍵在多個文件中搜索并替換指定內(nèi)容。
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.日志文件分析自動化通過Python解析日志文件,提取關(guān)鍵信息進(jìn)行統(tǒng)計分析。
def analyze_log(log_file):
    with open(log_file, '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.數(shù)據(jù)可視化自動化利用Matplotlib庫,實現(xiàn)數(shù)據(jù)的自動圖表生成。
import matplotlib.pyplot as plt
import pandas as pd
def visualize_data(data_file):
    df = pd.read_csv(data_file)
    
    # 示例:繪制柱狀圖
    df.plot(kind='bar', x='category', y='value')
    plt.title('數(shù)據(jù)分布')
    plt.xlabel('類別')
    plt.ylabel('值')
    plt.show()
# 使用示例:
visualize_data('data.csv')11.郵件附件批量下載通過Python解析郵件,自動化下載所有附件。
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('your-email@example.com', 'your-password', 'imap.example.com')12.定時發(fā)送報告自動化根據(jù)數(shù)據(jù)庫或文件內(nèi)容,自動生成并定時發(fā)送日報/周報。
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):
    # 假設(shè)這里是從數(shù)據(jù)庫或文件中獲取數(shù)據(jù)并生成報告內(nèi)容
    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', 'receiver@example.com', '每日數(shù)據(jù)報告')
# 結(jié)合前面的定時任務(wù)腳本,可實現(xiàn)定時發(fā)送功能13.自動化性能測試使用Python的locust庫進(jìn)行API接口的壓力測試。
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  # 驗證返回狀態(tài)碼為200
    @task(3)  # 指定該任務(wù)在總?cè)蝿?wù)中的執(zhí)行頻率是其他任務(wù)的3倍
    def post_data(self):
        data = {"key": "value"}
        response = self.client.post("/api/submit", json=data)
        assert response.status_code == 201  # 驗證數(shù)據(jù)成功提交后的響應(yīng)狀態(tài)碼
# 運行Locust命令啟動性能測試:
# locust -f your_test_script.py --host=http://your-api-url.com14、自動化部署與回滾腳本使用Fabric庫編寫SSH遠(yuǎn)程部署工具,這里以部署Django項目為例:
from fabric import Connection
def deploy(host_string, user, password, project_path, remote_dir):
    c = Connection(host=host_string, user=user, connect_kwargs={"password": password})
    with c.cd(remote_dir):
        c.run('git pull origin master')  # 更新代碼
        c.run('pip install -r requirements.txt')  # 安裝依賴
        c.run('python manage.py migrate')  # 執(zhí)行數(shù)據(jù)庫遷移
        c.run('python manage.py collectstatic --noinput')  # 靜態(tài)文件收集
        c.run('supervisorctl restart your_project_name')  # 重啟服務(wù)
# 使用示例:
deploy(
    host_string='your-server-ip',
    user='deploy_user',
    password='deploy_password',
    project_path='/path/to/local/project',
    remote_dir='/path/to/remote/project'
)
# 對于回滾操作,可以基于版本控制系統(tǒng)實現(xiàn)或創(chuàng)建備份,在出現(xiàn)問題時恢復(fù)上一版本的部署。














 
 
 

















 
 
 
 