Ketika saya mencoba menjalankan kode ini apa adanya, saya mendapatkan kesalahan ini "IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2147221008, 'CoInitialize belum dipanggil.', Tidak Ada, Tidak Ada )" , namun jika saya menjalankan stp_tracker sendiri, itu berfungsi dengan baik dan jika saya menjalankan notify stp sendiri, itu berfungsi dengan baik. Saya menghargai masukan siapa pun. Terima kasih
import time
import win32com.client
# import sys
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# import watchdog
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.stp", "*.step", "*.txt"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
# the file will be processed there
print(event.src_path, event.event_type)
def on_modified(self, event):
self.process(event)
notify_stps()
def on_created(self, event):
self.process(event)
notify_stps()
def on_deleted(self, event):
self.process(event)
notify_stps()
def stp_tracker():
# /if __name__ == '__main__':
path = r"W:\TestFolder"
observer = Observer()
observer.schedule(MyHandler(), path)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def notify_stps():
const = win32com.client.constants
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "I AM SUBJECT!!"
newMail.Body = "Step files in directory"
# newMail.BodyFormat = 2 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
# newMail.HTMLBody = "<HTML><BODY>Enter the <span style='color:red'>message</span> text here.</BODY></HTML>"
newMail.To = '[email protected]'
# attachment1 = r"C:\Temp\example.pdf"
# newMail.Attachments.Add(Source=attachment1)
newMail.Send()
stp_tracker()
pythoncom.CoInitialize()
, lihat: CoInitialize belum dipanggil - person Strive Sun   schedule 08.04.2020