我们说我有一个脚本可以检查这个文件夹,看看那里有什么我想要的东西。
我想让这个脚本在后台运行(这会更酷)或者每2个小时执行一次,如果发现任何事情,请给我发电子邮件。
我该怎么做?
答案 0 :(得分:1)
所以你基本上有两个部分:
1)如何设置我的预定脚本?
假设您使用的是Windows,则可以按照此处的步骤进行操作:Schedule Python Script - Windows 7 通过gui或命令行设置预定脚本。 该脚本的输出本质上可以是布尔值,并提示执行问题的第二部分。
2)如何发送电子邮件?
为此你可以试试这段代码:
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
此步骤的更多信息可在此处找到:How to send an email with Python?