想要实现python定时压缩指定文件夹并发送邮件,需要以下步骤:
1. 安装依赖库
利用Python的第三方库可方便实现上述需求,安装所需库:
pip install schedule
pip install zipfile
pip install smtplib
2. 编写压缩函数
我们需要编写一个函数来实现将文件夹(包含其内部所有文件)进行压缩的功能:
from zipfile import ZipFile
def compress_folder(folder_path, zip_path):
with ZipFile(zip_path, 'w') as zip:
# 将文件夹下的所有文件递归加入Zip文件中
for root, dirs, files in os.walk(folder_path):
for file in files:
zip.write(os.path.join(root, file))
代码解析:
with ZipFile(zip_path, 'w') as zip:
:利用zipfile库创建一个新的Zip文件。for root, dirs, files in os.walk(folder_path):
:利用os库的walk函数遍历文件夹,其中,root为当前目录的路径,dirs为当前目录下的所有子目录(不含子目录下的子目录),files为当前目录下的所有文件。zip.write(os.path.join(root, file))
:将每个文件添加到Zip文件中。
3. 编写发送邮件函数
我们需要编写一个发送邮件的函数,以便定时将Zip文件通过邮件发送:
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(sender_email, sender_password, receiver_email, subject, body, file_path):
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
with open(file_path, 'rb') as attachment:
file = MIMEApplication(attachment.read(), _subtype='zip')
file.add_header('Content-Disposition', 'attachment', filename=attachment.name)
message.attach(file)
smtp_server = smtplib.SMTP('smtp.gmail.com', 587)
smtp_server.starttls()
smtp_server.login(sender_email, sender_password)
smtp_server.sendmail(sender_email, receiver_email, message.as_string())
smtp_server.quit()
代码解析:
MIMEMultipart()
:创建一个多组件的邮件,可以添加附件及HTML内容。message.attach(MIMEText(body, 'plain'))
:添加邮件正文。with open(file_path, 'rb') as attachment:
:读取压缩后的Zip文件。file = MIMEApplication(attachment.read(), _subtype='zip')
:创建一个MIMEApplication对象,并将Zip文件内容作为MIMEApplication的附件。file.add_header('Content-Disposition', 'attachment', filename=attachment.name)
:设置Zip文件的Content-Disposition属性。smtp_server = smtplib.SMTP('smtp.gmail.com', 587)
:创建一个SMTP对象,设置SMTP服务器地址和端口号。smtp_server.starttls()
:如果SMTP服务器支持TLS加密,则对SMTP服务器进行加密。smtp_server.login(sender_email, sender_password)
:登录SMTP服务器。smtp_server.sendmail(sender_email, receiver_email, message.as_string())
:发送邮件。smtp_server.quit()
:退出SMTP服务器。
4. 定时执行
我们可以使用schedule库来实现定时执行压缩和发送邮件的函数:
import os
import time
import schedule
def task():
print('压缩与发送中...')
folder_path = 'your_folder_path'
zip_path = 'your_zip_path'
sender_email = 'your_sender_email'
sender_password = 'your_sender_password'
receiver_email = 'your_receiver_email'
subject = 'Your Subject'
body = 'Your Body'
compress_folder(folder_path, zip_path)
send_email(sender_email, sender_password, receiver_email, subject, body, zip_path)
print('压缩与发送结束')
schedule.every().day.at('20:00').do(task)
while True:
schedule.run_pending()
time.sleep(1)
代码解析:
schedule.every().day.at('20:00').do(task)
:表示每天20:00执行一次任务。while True: schedule.run_pending() time.sleep(1)
:不断执行所有已注册的任务。
示例
下面是一个例子,假设我们已经有一个包含多个文件的文件夹 /home/ubuntu/files/,我们想要每天晚上8点将其压缩并发送至收件人邮箱 myfriend@example.com,则设置代码中的相应参数,运行程序即可:
folder_path = '/home/ubuntu/files/'
zip_path = '/home/ubuntu/files.zip'
sender_email = 'your_email@gmail.com'
sender_password = 'your_email_password'
receiver_email = 'myfriend@example.com'
subject = '文件夹压缩并发送'
body = '您好,这是压缩后的文件夹。'
schedule.every().day.at('20:00').do(task)
while True:
schedule.run_pending()
time.sleep(1)
还有一个例子,我们想要将 Python 项目的代码压缩并每小时都发送到指定邮箱,则设置代码中的相应参数,运行程序即可:
folder_path = '/home/ubuntu/python_project/'
zip_path = '/home/ubuntu/python_project.zip'
sender_email = 'your_email@gmail.com'
sender_password = 'your_email_password'
receiver_email = 'your_email@example.com'
subject = 'Python项目打包'
body = '您好,这是Python项目源码。'
schedule.every().hour.do(task)
while True:
schedule.run_pending()
time.sleep(1)
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python实现定时压缩指定文件夹发送邮件 - Python技术站