[当我尝试按原样运行此代码时,出现此错误“ IDispatch = pythoncom.CoCreateInstance(IDispatch,None,clsctx,pythoncom.IID_IDispatch)pywintypes.com_error:(-2147221008,'尚未调用CoInitialize。',无,无)”,但是,如果我单独运行stp_tracker可以正常工作,而如果我单独运行通知stp则可以正常工作。我感谢任何人的投入。谢谢
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()
对此表示歉意,但是在互联网上搜索后,我发现有帮助。之前我碰到过同一篇文章,并认为它已被弃用,因为我在pycharm中的AutoComplete在键入pythoncom.CoInitialize()时未拾取任何内容,因此使我认为它已过时。也有相同的信息,Strive Sun解释过