import shutil
import os
import time
source_dir = '/path/to/source/directory'
backup_dir = '/path/to/backup/directory'
interval = 3600 # 备份间隔,单位秒
while True:
time.sleep(interval)
backup_folder = backup_dir + time.strftime('%Y%m%d%H%M%S')
shutil.copytree(source_dir, backup_folder)
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
def send_email(subject, content, to_addr):
mail_host = "smtp.example.com" # 邮件服务器地址
mail_user = "your_email@example.com" # 发件人邮箱
mail_pass = "your_email_password" # 发件人邮箱密码
sender = 'your_email@example.com' # 发件人邮箱
receivers = [to_addr] # 收件人邮箱
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header("Python自动化脚本", 'utf-8')
message['To'] = Header("用户", 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
try:
smtp_obj = smtplib.SMTP()
smtp_obj.connect(mail_host, 25)
smtp_obj.login(mail_user, mail_pass)
smtp_obj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
smtp_obj.quit()
except smtplib.SMTPException as e:
print("Error: 无法发送邮件", e)
# 设置定时发送
subject = "日报"
content = "今天的工作内容..."
to_addr = "recipient@example.com"
send_email(subject, content, to_addr)
以上是两个实用的Python自动化脚本的详解,希望对您有帮助。
本文链接:http://task.lmcjl.com/news/710.html