Python 自动化脚本实战:提升开发效率的10个技巧
Python 是自动化脚本的首选语言,语法简洁、生态丰富。这篇文章分享 10 个实用的 Python 自动化技巧,帮你提升开发效率。
1. 批量重命名文件
import os
def batch_rename(folder, old_ext, new_ext):
for filename in os.listdir(folder):
if filename.endswith(old_ext):
new_name = filename.replace(old_ext, new_ext)
os.rename(
os.path.join(folder, filename),
os.path.join(folder, new_name)
)
# 使用
batch_rename('./docs', '.txt', '.md')2. 自动发送邮件
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, to_email):
msg = MIMEText(body, 'html', 'utf-8')
msg['Subject'] = subject
msg['From'] = 'sender@example.com'
msg['To'] = to_email
with smtplib.SMTP_SSL('smtp.example.com', 465) as server:
server.login('username', 'password')
server.send_message(msg)更多技巧...
关于我们
尊云科技提供专业的 Python 开发服务,微信:yvsm316,QQ:316430983,官网:zunyun.cc
