如何解决如何让我的 python 看门狗脚本在 Outlook 中向我发送电子邮件通知以进行文件更新、删除、创建?
嗨,我正在尝试编写一个脚本,让我知道正在更新哪个特定文件,然后在 Outlook 中向我发送相同的电子邮件 -
我的脚本 -
import sys
import os
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import glob
def on_created(event):
os.chdir("C:/Users/yaoarian/Desktop/folder")
list_of_files = glob.glob("*.csv")
list_of_files.sort(key=os.path.getmtime)
last_file = list_of_files[len(list_of_files)-1]
print(last_file)
#print(last_file)
print("created")
def on_deleted(event):
print("deleted")
def on_modified(event):
print("modified")
def on_moved(event):
print("moved")
if __name__ == "__main__":
event_handler = FileSystemEventHandler()
# calling functions
event_handler.on_created = on_created
event_handler.on_deleted = on_deleted
event_handler.on_modified = on_modified
event_handler.on_moved = on_moved
path = "C:/Users/yaorian/Desktop/folder"
observer = Observer()
observer.schedule(event_handler,path,recursive=True)
observer.start()
try:
print("Monitoring")
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
print("terminate")
observer.join()
然而,这个脚本有效,我希望它向我发送有关文件更新的电子邮件警报。我尝试用 powershell 脚本做同样的事情,但没有收到警报
$uncPath = '\\PMI0826\C:\Users\yaorian\Desktop\folder' # enter the UNC path here
$yesterday = (Get-Date).AddDays(-1).Date # set at midnight for yesterday
# get an array of full filenames for any file that was last updates in the last 24 hours
$files = (Get-ChildItem -Path $uncPath -Filter '*.*' -File |
Where-Object { $_.LastWriteTime -ge $yesterday }).FullName
if ($files) {
$message = 'These files were modified in the last 24 hours:{0}{1}' -f [Environment]::NewLine,($files -join [Environment]::NewLine)
$emailTo = 'joel.t@cfio.com'
}
else {
$message = 'There were no files modified in the last 24 hours'
$emailTo = 'joel.t@cfio.com'
}
# output on screen
Write-Host $message
# create a hashtable with parameters for Send-MailMessage
$mailParams = @{
From = 'joel.t@cfiio.com'
To = $emailTo
Subject = 'Something Wrong'
Body = $message
SmtpServer = 'PMI0336.corp.yesgroup.com'
# any other parameters you might want to use
}
# send the email
Send-MailMessage @mailParams
感谢您的帮助。我的计划是设置一个任务调度程序来每天运行我的 .py 应用程序。我的重点是让 python 看门狗脚本向我发送电子邮件,因为我没有使用 powershell 进行调试。谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。