使用 Python 在特定目录中更改文件的电子邮件通知 [英] Email notification on file change in particular directory with Python
问题描述
我想编写一个在特定目录中查找的函数对于文件,如果有新文件,它应该发送通知邮件.
I would like to script a function wich is looking in a particular directory for files, if there are new files, it should send out an notification email.
我已经准备了一个在目录中寻找新文件的脚本,它写有关新文件到控制台的通知.但现在我想通过电子邮件通知,只要有新文件到达.有人可以帮忙吗?
I already prepared a script which is looking for new files in a directory, it write the notification about a new file into the console. But now I would like to notified via email, as soon as there has a new file arrived. Could someone help?
import os, time
def run():
path_to_watch = "//D$:/testfolder/"
print "watching: " + path_to_watch
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
removed = [f for f in before if not f in after]
if added: print "Added: ", ", ".join (added)
if removed: print "Removed: ", ", ".join (removed)
before = after
time.sleep (10)
if __name__ == "__main__":
print run()
推荐答案
如果您设置了 SMTP 邮件服务器,这将非常简单(我假设您有一个邮件系统!).总共需要大约 10 行代码.这里有一个python 示例.
It's very simple if you have an SMTP mail server set up (i'm assuming you have a mail system!). Will take you about 10 lines of code in total. Here is a python example.
如果您有任何问题,我们将需要更多信息以提供进一步帮助.例如,您使用的是什么邮件系统 e.t.c.
If you have any problems, we will need more information to help further. For example, what mail system you are using e.t.c.
这篇关于使用 Python 在特定目录中更改文件的电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!