When I try to run this code as is I get this error “IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2147221008, ‘CoInitialize has not been called.’, None, None)” , however if I run stp_tracker alone it works fine and if I run notify stp’s alone it works just fine. I appreciate anyones input. Thanks
JavaScript
x
70
70
1
import time
2
import win32com.client
3
# import sys
4
from watchdog.observers import Observer
5
from watchdog.events import PatternMatchingEventHandler
6
# import watchdog
7
8
9
class MyHandler(PatternMatchingEventHandler):
10
patterns = ["*.stp", "*.step", "*.txt"]
11
12
def process(self, event):
13
"""
14
event.event_type
15
'modified' | 'created' | 'moved' | 'deleted'
16
event.is_directory
17
True | False
18
event.src_path
19
path/to/observed/file
20
"""
21
# the file will be processed there
22
print(event.src_path, event.event_type)
23
24
def on_modified(self, event):
25
self.process(event)
26
notify_stps()
27
28
def on_created(self, event):
29
self.process(event)
30
notify_stps()
31
32
def on_deleted(self, event):
33
self.process(event)
34
notify_stps()
35
36
37
def stp_tracker():
38
# /if __name__ == '__main__':
39
path = r"W:TestFolder"
40
observer = Observer()
41
observer.schedule(MyHandler(), path)
42
observer.start()
43
44
try:
45
while True:
46
time.sleep(1)
47
except KeyboardInterrupt:
48
observer.stop()
49
50
observer.join()
51
52
53
def notify_stps():
54
const = win32com.client.constants
55
olMailItem = 0x0
56
obj = win32com.client.Dispatch("Outlook.Application")
57
newMail = obj.CreateItem(olMailItem)
58
newMail.Subject = "I AM SUBJECT!!"
59
newMail.Body = "Step files in directory"
60
# newMail.BodyFormat = 2 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
61
# newMail.HTMLBody = "<HTML><BODY>Enter the <span style='color:red'>message</span> text here.</BODY></HTML>"
62
newMail.To = 'Acoker251@outlook.com'
63
# attachment1 = r"C:Tempexample.pdf"
64
# newMail.Attachments.Add(Source=attachment1)
65
66
newMail.Send()
67
68
69
stp_tracker()
70
Advertisement
Answer
Apologize for that, but searching the internet and I found something that helped. I came across the same post earlier and assumed it was deprecated info because my AutoComplete in pycharm was not picking anything up when typing pythoncom.CoInitialize() so it made me think it was outdated info. Also the same information Strive Sun explained