当前位置:首页 > 科技  > 软件

14个 Python 自动化实战脚本

来源: 责编: 时间:2024-05-16 09:08:12 266观看
导读1.批量文件重命名神器在工作中,我们常常需要对大量文件进行批量重命名,Python帮你轻松搞定!import osdef batch_rename(path, prefix='', suffix=''): for i, filename in enumerate(os.listdir(path)): new_n

p2A28资讯网——每日最新资讯28at.com

1.批量文件重命名神器在工作中,我们常常需要对大量文件进行批量重命名,Python帮你轻松搞定!p2A28资讯网——每日最新资讯28at.com

import osdef 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.自动发送邮件通知告别手动发送,用Python编写定时发送邮件的自动化脚本。p2A28资讯网——每日最新资讯28at.com

import smtplibfrom email.mime.text import MIMETextdef 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.定时任务自动化执行使用Python调度库,实现定时执行任务的自动化脚本。p2A28资讯网——每日最新资讯28at.com

import scheduleimport timedef job_to_schedule():    print("当前时间:", time.ctime(), "任务正在执行...")# 定义每天9点执行任务schedule.every().day.at("09:00").do(job_to_schedule)while True:    schedule.run_pending()    time.sleep(1)# 使用示例:# 运行此脚本后,每天上午9点会自动打印当前时间及提示信息

4.数据库操作自动化简化数据库管理,Python帮你自动化执行CRUD操作。p2A28资讯网——每日最新资讯28at.com

import sqlite3def create_connection(db_file):    conn = None    try:        conn = sqlite3.connect(db_file)        print(f"成功连接到SQLite数据库:{db_file}")    except Error as e:        print(e)    return conndef 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("数据插入成功!")    except sqlite3.Error as e:        print(e)# 使用示例:conn = create_connection('my_database.db')data = {'name': 'John Doe', 'age': 30}insert_data(conn, 'users', data)# 在适当时候关闭数据库连接conn.close()

5.网页内容自动化抓取利用BeautifulSoup和requests库,编写Python爬虫获取所需网页信息。p2A28资讯网——每日最新资讯28at.com

import requestsfrom bs4 import BeautifulSoupdef fetch_web_content(url):    response = requests.get(url)    if response.status_code == 200:        soup = BeautifulSoup(response.text, 'html.parser')        # 示例提取页面标题        title = soup.find('title').text        return title    else:        return "无法获取网页内容"# 使用示例:url = 'https://example.com'web_title = fetch_web_content(url)print("网页标题:", web_title)

6.数据清洗自动化使用Pandas库,实现复杂数据处理和清洗的自动化。p2A28资讯网——每日最新资讯28at.com

import pandas as pddef 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# 使用示例:cleaned_df = clean_data('data.csv')print("数据清洗完成,已准备就绪!")

7.图片批量压缩用Python快速压缩大量图片以节省存储空间。p2A28资讯网——每日最新资讯28at.com

from PIL import Imageimport osdef 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.文件内容查找替换Python脚本帮助你一键在多个文件中搜索并替换指定内容。p2A28资讯网——每日最新资讯28at.com

import fileinputdef 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解析日志文件,提取关键信息进行统计分析。p2A28资讯网——每日最新资讯28at.com

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.数据可视化自动化利用Matplotlib库,实现数据的自动图表生成。p2A28资讯网——每日最新资讯28at.com

import matplotlib.pyplot as pltimport pandas as pddef visualize_data(data_file):    df = pd.read_csv(data_file)        # 示例:绘制柱状图    df.plot(kind='bar', x='category', y='value')    plt.title('数据分布')    plt.xlabel('类别')    plt.ylabel('值')    plt.show()# 使用示例:visualize_data('data.csv')

11.邮件附件批量下载通过Python解析邮件,自动化下载所有附件。p2A28资讯网——每日最新资讯28at.com

import imaplibimport emailfrom email.header import decode_headerimport osdef 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.定时发送报告自动化根据数据库或文件内容,自动生成并定时发送日报/周报。p2A28资讯网——每日最新资讯28at.com

import pandas as pdimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartdef 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', 'receiver@example.com', '每日数据报告')# 结合前面的定时任务脚本,可实现定时发送功能

13.自动化性能测试使用Python的locust库进行API接口的压力测试。p2A28资讯网——每日最新资讯28at.com

from locust import HttpUser, task, betweenclass 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

14、自动化部署与回滚脚本使用Fabric库编写SSH远程部署工具,这里以部署Django项目为例:p2A28资讯网——每日最新资讯28at.com

from fabric import Connectiondef 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')  # 执行数据库迁移        c.run('python manage.py collectstatic --noinput')  # 静态文件收集        c.run('supervisorctl restart your_project_name')  # 重启服务# 使用示例:deploy(    host_string='your-server-ip',    user='deploy_user',    password='deploy_password',    project_path='/path/to/local/project',    remote_dir='/path/to/remote/project')# 对于回滚操作,可以基于版本控制系统实现或创建备份,在出现问题时恢复上一版本的部署。

本文链接:http://www.28at.com/showinfo-26-88357-0.html14个 Python 自动化实战脚本

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 字节面试:说说Java中的锁机制?

下一篇: Spring Boot 编写 API 的十条最佳实践

标签:
  • 热门焦点
  • 对标苹果的灵动岛 华为带来实况窗功能

    继苹果的灵动岛之后,华为也在今天正式推出了“实况窗”功能。据今天鸿蒙OS 4.0的现场演示显示,华为的实况窗可以更高效的展现出实时通知,比如锁屏上就能看到外卖、打车、银行
  • 0糖0卡0脂 旭日森林仙草乌龙茶优惠:15瓶到手29元

    旭日森林无糖仙草乌龙茶510ml*15瓶平时要卖为79.9元,今日下单领取50元优惠券,到手价为29.9元。产品规格:0糖0卡0脂,添加草本仙草汁,清凉爽口,富含茶多酚,保留
  • 掘力计划第 20 期:Flutter 混合开发的混乱之治

    在掘力计划系列活动第20场,《Flutter 开发实战详解》作者,掘金优秀作者,Github GSY 系列目负责人恋猫的小郭分享了Flutter 混合开发的混乱之治。Flutter 基于自研的 Skia 引擎
  • 破圈是B站头上的紧箍咒

    来源 | 光子星球撰文 | 吴坤谚编辑 | 吴先之每年的暑期档都少不了瞄准追剧女孩们的古偶剧集,2021年有优酷的《山河令》,2022年有爱奇艺的《苍兰诀》,今年却轮到小破站抓住了追
  • 2天涨粉255万,又一赛道在抖音爆火

    来源:运营研究社作者 | 张知白编辑 | 杨佩汶设计 | 晏谈梦洁这个暑期,旅游赛道彻底火了:有的「地方」火了——贵州村超旅游收入 1 个月超过 12 亿;有的「博主」火了&m
  • 猿辅导与新东方的两种“归途”

    作者|卓心月 出品|零态LT(ID:LingTai_LT)如何成为一家伟大企业?答案一定是对“势”的把握,这其中最关键的当属对企业战略的制定,且能够站在未来看现在,即使这其中的
  • 大厂卷向扁平化

    来源:新熵作者丨南枝 编辑丨月见大厂职级不香了。俗话说,兵无常势,水无常形,互联网企业调整职级体系并不稀奇。7月13日,淘宝天猫集团启动了近年来最大的人力制度改革,目前已形成一
  • 华为Mate 60保护壳曝光:硕大后置相机模组 凸起程度有惊喜

    这段时间以来,关于华为新旗舰的爆料日渐密集。据此前多方爆料,今年华为将开始恢复一年双旗舰战略,除上半年推出的P60系列外,往年下半年的Mate系列也将
  • OPPO K11搭载长寿版100W超级闪充:26分钟充满100%

    据此前官方宣布,OPPO将于7月25日也就是今天下午14:30举办新品发布会,届时全新的OPPO K11将正式与大家见面,将主打旗舰影像,和同档位竞品相比,其最大的卖
Top